Local Notifications in Android?

AndroidNotificationsLocal

Android Problem Overview


In iOS, a "Local Notification" is used by an app when it is in the background, to inform the user that something has happened, that they might want to pay attention to:

> Local notifications ... to inform users when new data becomes available for your app, even when your app is not running in the foreground. For example, a messaging app might let the user know when a new message has arrived, and a calendar app might inform the user of an upcoming appointment.

Apple dev - Local and Remote Notifications Overview

[Its "local" if the app itself is providing the new data; "remote" if a remote server is sending the update.]

Is there an equivalent on Android?

Android Solutions


Solution 1 - Android

Use NotificationCompat.Builder if you are targeting old APIs too.

    Intent intent = new Intent(ctx, HomeActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 
    NotificationCompat.Builder b = new NotificationCompat.Builder(ctx);

    b.setAutoCancel(true)
     .setDefaults(Notification.DEFAULT_ALL)
     .setWhen(System.currentTimeMillis())         
     .setSmallIcon(R.drawable.ic_launcher)
     .setTicker("Hearty365")            
     .setContentTitle("Default notification")
     .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
     .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
     .setContentIntent(contentIntent)
     .setContentInfo("Info");


    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, b.build());

Solution 2 - Android

If you want to fire local notification with big data i.e., with multiline text in single notification with title, Ticker, icon, sound.. use following code.. I think it will help you..

   Intent notificationIntent = new Intent(context,
            ReminderListActivity.class);



    notificationIntent.putExtra("clicked", "Notification Clicked");
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP); // To open only one activity


        // Invoking the default notification service 

        NotificationManager mNotificationManager;
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context);
        Uri uri = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setContentTitle("Reminder");
        mBuilder.setContentText("You have new Reminders.");
        mBuilder.setTicker("New Reminder Alert!");
        mBuilder.setSmallIcon(R.drawable.clock);
        mBuilder.setSound(uri);
        mBuilder.setAutoCancel(true);

        // Add Big View Specific Configuration 
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        String[] events = null;

            events[0] = new String("Your first line text ");
            events[1] = new String(" Your second line text");



        // Sets a title for the Inbox style big view
        inboxStyle.setBigContentTitle("You have Reminders:");

        // Moves events into the big view
        for (int i = 0; i < events.length; i++) {
            inboxStyle.addLine(events[i]);
        }

        mBuilder.setStyle(inboxStyle);

        // Creates an explicit intent for an Activity in your app 
        Intent resultIntent = new Intent(context,
                ReminderListActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder
                .create(context);
        stackBuilder.addParentStack(ReminderListActivity.class);


        // Adds the Intent that starts the Activity to the top of the stack


        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);

        mBuilder.setContentIntent(resultPendingIntent);
        mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);


        // notificationID allows you to update the notification later  on.


        mNotificationManager.notify(999, mBuilder.build());

Solution 3 - Android

LocalBroadcastManager looks like a better solution: http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html Create your own custom Intent action, broadcast it to your process, and make sure any activity etc. is registered as a receiver for that intent.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArsalan HaiderView Question on Stackoverflow
Solution 1 - AndroidSumoanandView Answer on Stackoverflow
Solution 2 - AndroidPratibha SarodeView Answer on Stackoverflow
Solution 3 - AndroidqixView Answer on Stackoverflow