0
    PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)(Math.random() * 100),MyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    //A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent

    mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText,pendingIntent);

    mNotification.flags |= Notification.FLAG_NO_CLEAR;
    notificationManager.notify(0,   mNotification);

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    MainActivity.this.finish();
    startActivity(intent);  

I need to save the notification in notification bar even after device is restarted.

1 Answers1

1

show notification when the device reboot you can use following approach. When the system reboots the notification tray becomes empty. There is no way to make such notification which stays in memory even when system shuts down. The only way to show persistent notification is to store data some where like database or shared preferences and show the same notification when system reboots.

Android BroadcastReceiver on startup - keep running when Activity is in Background

just write code inside onRecieve method which will be called when the device reboots.

First You need to define a receiver in manifest with action name android.intent.action.BOOT_COMPLETED.

<!-- Start the Service if applicable on boot -->
<receiver android:name="com.prac.test.ServiceStarter">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver

> Make sure also to include the completed boot permission.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;

    public class ServiceStarter extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

/* show notification here*/
        }
    }
Community
  • 1
  • 1
Nitin
  • 1,966
  • 4
  • 22
  • 53