Android Studio : Open app to MainActivity from notification API 31 - java

I have been trying to open my application to my MainActivity, and load a fragment, from a notification and I cannot seem to do so successfully. My most successful build appears to do everything successfully except actually open the app.
The app will be completely closed and I'll receive the notification and tap on it. The phone drop down menu will retract and act like it's about to open the app, then doesn't. When I open the app manually the application has been create and navigated successfully to the fragment. It just didn't open my app.
I'll list relevant code below:
Manifest Code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.easy.planner">
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:theme="#style/Theme.Planner"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>=
<receiver
android:name=".ReminderReceiver"
android:exported="true"
android:theme="#android:style/Theme.NoDisplay"/>
</application>
</manifest>
Code in ReminderReceiver
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP & Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("id", reminder.getId());
final PendingIntent pendingIntent;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
pendingIntent = PendingIntent.getActivity(context, reminder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);}
else{
pendingIntent = PendingIntent.getActivity(context, reminder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);}
NotificationHelper helper = new NotificationHelper(context);
String title = "title";
String message = "message";
NotificationCompat.Builder builder = helper
.getReminderNotification(context, title, message,pendingIntent, true);
helper.GetManager().notify(reminder.getId(), builder.build());
Notification Helper
public class NotificationHelper extends ContextWrapper {
public static final String TAG = "Notification Helper";
public static final String REMINDERS_CHANNEL_ID = "id";
public static final String REMINDERS_CHANNEL_NAME = "name";
private NotificationManager manager;
public NotificationHelper(Context base) {
super(base);
CreateChannels(base);
}
public static boolean CreateChannels(Context context)
{
NotificationChannel reminders = new NotificationChannel(REMINDERS_CHANNEL_ID, REMINDERS_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
reminders.enableLights(true);
reminders.enableVibration(true);
NotificationManager manager = GetManager(context);
manager.createNotificationChannel(reminders);
return true;
}
public NotificationCompat.Builder getReminderNotification(Context context, String title, String message, PendingIntent pendingIntent, boolean autoCancel)
{
return new NotificationCompat.Builder(getApplicationContext(), REMINDERS_CHANNEL_ID)
.setSmallIcon(R.drawable.notification_temp)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.setAutoCancel(autoCancel);
}
I've been looking at plenty of other stackoverflow pages and have yet to find a solution.

you can update your ReminderReceiver's code to this and check if it is working
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP &
Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("id", reminder.getId());
final PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(context, reminder.getId(), notificationIntent /*update*/ , PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, reminder.getId(), notificationIntent /*update*/ , PendingIntent.FLAG_UPDATE_CURRENT);
}
NotificationHelper helper = new NotificationHelper(context);
String title = "title";
String message = "message";
NotificationCompat.Builder builder = helper
.getReminderNotification(context, title, message, pendingIntent, true);
helper.GetManager().notify(reminder.getId(), builder.build());
I think you are apssing wrong intent into your NotificationBuilder.
Hope this works!!

Related

Notification Builder action not working on API 31 (Android 12)

I'm stuck while dealing with this api 31. i made a chat app. When I create an action button to receive a phone call it works on android below API 31 or below Android 12, but it doesn't work on Android 12 (only work if the notification is not has action). maybe someone here can help me in finding a solution to this problem?
FirebaseService.java
#Override
public void onMessageReceived(#NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
boolean cancelNotification = false;
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Map<String, String> params = remoteMessage.getData();
JSONObject data = new JSONObject(params);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE);
}
else {
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
}
String channel_id = getNotificationData(data, "channel");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channel_id)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getNotificationData(data, "title"))
.setContentText(getNotificationData(data, "body"))
.setAutoCancel(true)
.setContentIntent(pendingIntent);
if(channel_id.equals("cll2") && getNotificationData(data, "call_action").equals("open")){
playCallSound(this);
if(WindowUtil.isAppOnBackground(this) || isScreenOff()){
ArrayList<String> par = new ArrayList<>();
par.add("call_channel");
par.add("call_type");
par.add("call_token");
par.add("call_peer");
String callType = getNotificationData(data, "call_type");
if(callType.equals("3") || callType.equals("4")){
par.add("call_group_name");
par.add("call_group_caller");
}
Intent receiveCallAction = new Intent(this, CallReceiver.class);
receiveCallAction.putExtra("action", "ANSWER");
Intent cancelCallAction = new Intent(this, CallReceiver.class);
cancelCallAction.putExtra("action", "DECLINE");
for (String s : par) {
receiveCallAction.putExtra(s, getNotificationData(data, s));
cancelCallAction.putExtra(s, getNotificationData(data, s));
}
notificationBuilder.setOngoing(true);
receiveCallAction.setAction("RECEIVE_CALL");
cancelCallAction.setAction("CANCEL_CALL");
PendingIntent receiveCallPendingIntent = PendingIntent.getBroadcast(this, 1200, receiveCallAction, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent cancelCallPendingIntent = PendingIntent.getBroadcast(this, 1201, cancelCallAction, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder
.addAction(R.drawable.ic_baseline_call_24_green, "Answer", receiveCallPendingIntent)
.addAction(R.drawable.ic_baseline_call_red_24, "Decline", cancelCallPendingIntent);
}else{
cancelNotification = true;
}
}if(channel_id.equals("cll2") && getNotificationData(data, "call_action").equals("end_timeout")){
stopRinging();
}else if(channel_id.equals("ntf1")){
if(UserUtil.IS_CHAT_ACTIVITY){
cancelNotification = true;
}
if(!cancelNotification){
playNotificationSound(this);
}
}
if(!cancelNotification){
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
NotificationChannel channel = new NotificationChannel(channel_id,
getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.enableVibration(true);
mNotificationManager.createNotificationChannel(channel);
}
mNotificationManager.notify(1000, notificationBuilder.build());
}
}
CallReceiver.java (Broadcast Receiver)
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getExtras() != null) {
String action = intent.getStringExtra("action");
// Perform the action
performClickAction(context, action, intent.getExtras());
// Close the notification after the click action is performed.
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(1000);
// Stop the ringtone
stopRinging();
}
}
on my AndroidManifest.xml
<service
android:name=".services.FirebaseService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<receiver
android:name=".receiver.CallReceiver"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="CALL_RECEIVER" />
</intent-filter>
</receiver>
Thank you
i solve this problem by this code
PendingIntent receiveCallPendingIntent;
PendingIntent cancelCallPendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
receiveCallPendingIntent = PendingIntent.getBroadcast(this, 1200, receiveCallAction, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
cancelCallPendingIntent = PendingIntent.getBroadcast(this, 1201, cancelCallAction, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
}else{
receiveCallPendingIntent = PendingIntent.getBroadcast(this, 1200, receiveCallAction, PendingIntent.FLAG_UPDATE_CURRENT);
cancelCallPendingIntent = PendingIntent.getBroadcast(this, 1201, cancelCallAction, PendingIntent.FLAG_UPDATE_CURRENT);
}
also add this permission for force Android 12^ (AndroidManifest.xml)
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><receiver
android:name=".receiver.CallReceiver"
android:exported="false"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

Start ForegroundService on Boot api 29

I try to start a ForegroundService by an BroadcastReceiver on Boot. I have tried this on a device with Android 8 and on an emulated Android 10 device. Both work fine. The problem is, that this code does not work on my Android 10 Galaxy S9. I have debugged everything, my BroadcastReceiver and the OnStartCommand method but also if the BroadcastReceiver runs the code fine, the OnStartCommand method does not start running.
I have a mainActivity in which the user has to apply this Process so the app gets opened once before the BroadcastReceiver should work.
I tried almost every solution i found here but nothing worked for me.
I don't understand why this is working on some devices but not on each. I hope someone can help me!
My min API is 26.
BroadcastReceiver
public class Restarter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intentt = new Intent (context, MyService.class);
intentt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startForegroundService(intentt);
} else {
Intent intentt = new Intent (context, MyService.class);
intentt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(intentt);
}
}
}
Service
public class MyService extends Service {
public static final String CHANNEL_ID = "exampleServiceChannel";
#Override
public void onCreate() {
super.onCreate();
Log.d("WhereAreWe", "create");
createNotificationChannel();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d("WhereAreWe", "StartCommand");
SharedPreferences check = getSharedPreferences("Checked", Context.MODE_PRIVATE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O){
Intent intents = new Intent(this, MainActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intents, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "newid")
.setSmallIcon(R.drawable.ic_android)
.setContentTitle("Foreground Service")
.setContentText("Example text")
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent);
startForeground(1, builder.build());
}else{
startForeground(1, new Notification());
}
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.MyApplication">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
.
.
.
<receiver
android:name=".Restarter"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="restartservice" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"
android:stopWithTask="false" />
</application>
</manifest>

Android: Background execution not allowed: receiving Intent

I am trying to send a notification using Alarm Manager, the code is working fine on the lower SDK versions like 26 below. The android implicit background ban is not letting the notification to broadcast.
find code below for BroadcastReceiver :
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("AlarmReceiver-Worked");
MainActivity.initNotificationChannels(context);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default")
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(intent.getStringExtra("title"))
.setContentText(intent.getStringExtra("text"))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Manifext.xml
<receiver android:name="com.x.Controllers.Notification.AlarmReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
first of all
not able to go past this error
which error? no log, no info what isn't working... for which actions this BroadcastReceiver is registered? is this for BOOT_COMPLETED or smth else?
besides that
private static boolean notificationChannelsInitialized = false;
if (notificationChannelsInitialized) {
return;
}
it will always return in here, you are setting this flag to true later in this method, it will never happen.
also don't use static flag, just check that Channel already exists
NotificationChannel channel = manager.getNotificationChannel("default");
notificationChannelsInitialized = Build.VERSION.SDK_INT < 26 || channel != null;
on API below 26 just assume it is there
Intent notificationIntent = new Intent(context, AlarmReceiver.class);
notificationIntent.setAction("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.putExtra("title", notificationData.getString("title"));
notificationIntent.putExtra("text", notificationData.getString("text"));
PendingIntent broadcast = PendingIntent.getBroadcast(this.context, i, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);

Don't show notifications Android

I'm trying to learn how to develop applications for Android devices. For some time I have been developing an application in which there is a function for sending notifications (reminders) at different times of the day. But if the phone is locked and the time to send the notification is more than 10 minutes then they do not come. Tried BroadcasrReceiver:
public class NotificationReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
addNotif(context, "Times UP Receiver", "5 second Receiver", "Alert Receiver");
}
private void addNotif(Context context, String msg, String msgText, String msgAlert) {
Notification.Builder builder = new Notification.Builder(context);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent notifIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setPriority(Notification.PRIORITY_HIGH).setContentIntent(notifIntent)
.setSmallIcon(R.drawable.ic_android_black_24dp)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_android_black_24dp))
.setTicker(msgAlert)
.setWhen(System.currentTimeMillis())
.setContentTitle(msg)
.setContentText(msgText);
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_ALL;
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(101, notification);
}
}
use AlarmManager:
Calendar calNotif = Calendar.getInstance();
Intent alertIntent = new Intent(getBaseContext(), NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 102, alertIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager notifAlarm = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
notifAlarm.set(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis() + (15 * 60 * 1000), pendingIntent);
I also tried to use Service:
public class NotificationService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Calendar calNotif = Calendar.getInstance();
Intent alertIntent = new Intent(getBaseContext(), NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 103, alertIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager notifAlarm = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
notifAlarm.set(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis() + (16 * 60 * 1000), pendingIntent);
return START_STICKY;
}
}
Manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".NotificationReceiver" />
<service android:name=".NotificationService" />
But nothing works. My head hurts, I still can not understand why notifications do not come. Tell me please what's the problem.
Don't use BaseContext and your going to test notification then why should you delayed 15 mins for notification. Minimize your time and check after its works increase your time.
In YourActivity
Calendar calNotif = Calendar.getInstance();
Intent alertIntent = new Intent(this, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 102, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager notifAlarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN)
notifAlarm.setExact(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis()+10000, pendingIntent);
else
notifAlarm.set(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis()+10000, pendingIntent);
In your Notification Code
builder.setPriority(Notification.PRIORITY_HIGH).setContentIntent(notifIntent)
.setSmallIcon(R.drawable.ic_android_black_24dp)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_android_black_24dp))
.setTicker(msgAlert)
.setWhen(System.currentTimeMillis())
.setContentTitle(msg)
.setContentText(msgText);
Remove setLargeIcon beacause it uses Bitmap and takes more memory. After its worked you can implement using service.

Android service not working after reboot, giving ANR error

Went through the documentation on how to start a service automatically after the device boots and tried all they said but couldn't get my service to work after rebooting the device, though it works perfectly well on first instance.
Below is my main activity (MainActivity.java) which starts the service after a button has been clicked, and triggers the service every 10 seconds using AlarmManager.
public void onClickSubmitDate(View view) {
Intent service = new Intent(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, service, 0);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, 10 * 1000, pendingIntent);
//Enable receiver when device boots
ComponentName receiver = new ComponentName(this, BootReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
Below is my receiver class (BootReceiver.java)
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent service = new Intent(context, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(null, 0, service, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
10 * 1000, 10 * 1000,
pendingIntent);
}
}
}
My service contains a thread which checks for multiple scenarios and builds a notification for each scenario. Here's a sample below:
public int onStartCommand(Intent intent, int flags, int startId) {
notification = new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
Runnable r = new Runnable() {
#Override
public void run() {
try {
boolean diff = true;
if (diff) {
// Build the notification
notification.setSmallIcon(R.drawable.icon);
notification.setTicker(getString(R.string.notification_ticker));
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle(getString(R.string.notification_title));
notification.setContentText(getString(R.string.notification_1_text));
notification.setSound(alarmSound);
Intent i = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
// Builds a notification and issues it
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Random r = new Random();
int rand = r.nextInt(1000);
nm.notify(rand, notification.build());
}
catch (Exception e) {
e.printStackTrace();
}
}
};
Thread stephThread = new Thread(r);
stephThread.start();
return START_STICKY;
}
And finally below is my AndroidManifest.xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.motherslove.stephnoutsa.myapplication18calendartest"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyDate"
android:label="#string/title_activity_my_date"
android:theme="#style/AppTheme.NoActionBar" />
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" />
<receiver
android:name=".BootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Please can someone tell me what I've done wrong?
Thanks to help from #MikeM I could fix the problem.
Instead of
PendingIntent pendingIntent = PendingIntent.getService(null, 0, service, 0);
I had to pass in the context as the first argument, in the receiver, as shown below
PendingIntent pendingIntent = PendingIntent.getService(context, 0, service, 0);

Categories