56

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?

ToolmakerSteve
  • 5,893
  • 8
  • 67
  • 145
Arsalan Haider
  • 591
  • 1
  • 5
  • 7
  • 3
    This appears to be the oldest asking of this question; it is linked to from at least one later version of the question, and variants of the question have been asked at least 4 times. Therefore, I have rewritten it to make it less vague, and voted to reopen it. IMHO, it is a fundamental mobile feature; the question deserves to be asked and answered. [Alternatively, could close it as a duplicate of a later variant, if that has better answers, since no answer was accepted here. Google "stackoverflow android local notification"] – ToolmakerSteve Mar 25 '19 at 20:20
  • 1
    Two useful answers on other threads: https://stackoverflow.com/a/8590724/199364, https://stackoverflow.com/a/31946656/199364 - from these we see that Android doesn't have the direct equivalent of the iOS feature "local notification". Several different Android APIs are mentioned as possibly involved in a solution. Therefore I understand if the consensus is that this is still "too broad", though I personally would like to see, gathered in one place, possible alternatives for "app in background, how should we tell user something important has happened?" – ToolmakerSteve Mar 25 '19 at 20:35

3 Answers3

53

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());
Sumoanand
  • 8,343
  • 1
  • 45
  • 45
  • 1
    Good working example. I followed other sample code and it worked with lollipop or greater versions. But this code works on kitkat also (I don't know why... maybe the Icon or defaults?) Thanks – po5i Nov 29 '16 at 19:52
6

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());
Pratibha Sarode
  • 1,583
  • 14
  • 16
5

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.

qix
  • 6,008
  • 1
  • 44
  • 58
  • My understanding is: An iPhone local notification, is a communication from your app to the user. LocalBroadcastManager on Android is a communication from one part of your app to another part of your app. I don't think LocalBroadcastManager is relevant to the question, but correct me if I'm wrong. – ToolmakerSteve Mar 25 '19 at 20:07