Local Notifications in Android?

Arsalan Haider picture Arsalan Haider · Feb 22, 2011 · Viewed 90.2k times · Source

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?

Answer

Sumoanand picture Sumoanand · Dec 31, 2014

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());