My problem is I want to cancel an ongoing notification only if cancel option is selected in the app, but whenever I clear memory on "Task Manager/RAM" tab of my android device my notification disappears.
What can I do to make my ongoing notification stay even if I clear memory?
I created an ongoing notification in a class that extends BroadcastReciever
class.In my OnGoingNotificationReciever(extends BroadcastReciever):
Intent mainAct = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 555, mainAct, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(resID).setContentTitle(city).setContentText(temperature);
mBuilder.setContentIntent(contentIntent);
mBuilder.setOngoing(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
Then I set an AlarmManager
to start this BroadcastReciever and repeat every 5 minutes:
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent i7 = new Intent(getApplicationContext(), OnGoingNotificationReciever.class);
PendingIntent ServiceManagementIntent = PendingIntent.getBroadcast(getApplicationContext(), 1111111, i7, 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime(), 5 * 60 * 1000, ServiceManagementIntent);
This works perfectly and does what I want to do(e.g when I close the app, notification stays on the status bar) except it disappears when clearing memory.
Note: In Yahoo Weather application it does what I want to do, but I couldn't find how on the Internet.
After a lot of search I finally found a solution to this problem.
In my Manifest.xml I wrote the following:
<receiver android:name=".OnGoingNotificationReciever" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
<data android:scheme="package" android:path="com.example.havadurumumenu" />
</intent-filter>
</receiver>
PACKAGE_RESTARTED
is called when you clear memory, and setting this restarts your receiver after clearing memory.