2

I have this code that works great:

Notification n = builder.build();
n.flags = Notification.FLAG_NO_CLEAR;

But when I restart the phone, the notification goes away. Is there any flag that make that happen?

EGHDK
  • 17,818
  • 45
  • 129
  • 204

2 Answers2

1

No. I don't think that is possible.

You could have a service that runs at start-up to to bring up that notification again. Notifications otherwise do not persist across reboots.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • Let's say the text in the notification was saved as a variable, how would it know that text? Would I have to save the message into a database? – EGHDK Oct 16 '12 at 01:31
  • A database would be overkill for a notification. You can use [SharedPreferences](http://developer.android.com/guide/topics/data/data-storage.html#pref). – Anirudh Ramanathan Oct 16 '12 at 01:32
  • A database is just one method of persistent storage, but yes, you would need to save it somehow. – jonvuri Oct 16 '12 at 01:32
  • Gotcha. But a service would have to start to bring up those notifications though... right? – EGHDK Oct 16 '12 at 01:33
  • 1
    Yes. You can get it to load when Boot is complete. There is a detailed answer [**here**](http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android) that can help you do that. – Anirudh Ramanathan Oct 16 '12 at 01:35
  • @EGHDK you don't need a service, just set the notification after receiving the broadcast. – vikki Oct 16 '12 at 04:25
  • @vikki what do you mean by broadcast? – EGHDK Oct 16 '12 at 20:29
  • @EGHDK a broadcast is a message sent out when an event occurs. The message is received by a [BroadcastReceiver](http://developer.android.com/reference/android/content/BroadcastReceiver.html). in the link posted by Cthulhu in his comment above mine, in the accepted answer a broadcastReceiver is created and registered to be informed when android boots up, in the answer, a service is created when that message is received, for you, you shouldn't start a service, just create the notification there – vikki Oct 19 '12 at 07:28
1

If you want to print notification when the device boots up, you can create a receiver that is invoked when the system boot is completed, for this, first create a receiver,

public class MyReciever extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) 
{
        Log.d("BOOT COMPLETE","SERVICE CALLED>>>>>>>>>>>>");
        //use your code here to print notifications

    }
}

This receiver is invoked when the system boot is completed. You can also call a service from the onReceive method of receiver to print the notification.

Also you must define the following regularities in your manifest file,

First define permission for getting BOOT_COMPLETION intent,

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

Then define your receiver also,

 <receiver android:name=".MyReciever" 
             android:enabled="true" 
             android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100