1

I am using the following code to add an icon to Notification status, it works well, it always display even if I exit the app.

But the icon disappears whenever I restart mobile phone, what can I do to make the icon always display even if I restart phone ?

private void showNotification() {

    NotificationManager notificationManager = (NotificationManager)
    this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);


    Notification notification = new Notification(R.drawable.smsforward,"My System", System.currentTimeMillis());


    notification.flags |= Notification.FLAG_ONGOING_EVENT; 
    notification.flags |= Notification.FLAG_NO_CLEAR;

    CharSequence contentTitle = "My System Title"; 
    CharSequence contentText = "My System Title content"; 

    Intent notificationIntent = new Intent(this, SMSMain.class); 

    PendingIntent contentItent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    notification.setLatestEventInfo(this, contentTitle, contentText,contentItent);

    notificationManager.notify(0, notification);
}
sakhunzai
  • 13,900
  • 23
  • 98
  • 159
HelloCW
  • 843
  • 22
  • 125
  • 310

3 Answers3

2

You need to listen when the boot is completed and then show same notification again. Here is a simple example.

In your manifest file.

<receiver android:name="BootNotificationReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

And in your code.

 public class BootNotificationReceiver extends BroadcastReceiver
    {
    public void onReceive(Context context, Intent intent) {

     // display notification again
     showNotification();// or whatever

      }
    }

Happy Coding :)

Adnan
  • 5,025
  • 5
  • 25
  • 44
1

You can handle the device boot up intent and launch the same notification again.

Android BroadcastReceiver, auto run service after reboot of device

Community
  • 1
  • 1
Ajit Pratap Singh
  • 1,299
  • 12
  • 24
1

Declare Broadcast Receiver in manifest xml.

public class BootReceiver extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context ctx, Intent intent) {
            if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
            {
                // your code here
            }
        }
    }
Vinothkumar Arputharaj
  • 4,567
  • 4
  • 29
  • 36