Latha
Latha

Reputation: 135

How can keep notification in notification area/ bar if device shutdown?

I am trying to implement push notifications and I got notifications too. But now I need to notifications in notification bar when switched on. I.e. if I got notification when mobile is on at time we did not see notification that is in notification area, if device shutdown then when I switched on the mobile, at time need to get that notification on notification bar and I have also another requirement, i.e. if I remove notification in notification area then after 10 minutes I need get that notification in notification area/ bar.

How can I accomplish this?

Upvotes: -1

Views: 111

Answers (3)

Haven
Haven

Reputation: 546

  1. You need to save notification content some where such as SharePreference.
  2. You listen when device booted by using this intent, "android.intent.action.BOOT_COMPLETED", when broadcast receiver get fired, you read notication content from step 1, fire notification again.

Easy, right :)

Upvotes: 3

Bui Quang Huy
Bui Quang Huy

Reputation: 1794

You can use PendingIntent with FLAG_ONE_SHOT :

private void sendNotification(String from, String message) {
        Bundle bundle = new Bundle();
        Intent intent = new Intent(this, ChatActivity.class);
        intent.putExtra("INFO", bundle);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setContentTitle(from)
                .setSound(defaultSoundUri)
                .setContentText(message)
                .setSmallIcon(R.drawable.ic_notification)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, builder.build());
    }

And remember set WAKE_LOCK permission in your manifest file:

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

Upvotes: 1

F43nd1r
F43nd1r

Reputation: 7759

To do stuff on boot register a BroadcastReceveiver to BOOT_COMPLETED in your manifest.

To do stuff after a certain time use AlarmManager.

Upvotes: 0

Related Questions