Query regarding Android push notifications

2019-08-21 03:06发布

问题:

I have an Android app with a working push notification set up. It works currently like this:

  1. User gets a push notification
  2. When the user taps on the push notification, he is taken to a screen which has the list of all the notifications. It is basically a list view which has all the notifications.

Now, want to implement the following:

a. When a new notification comes to the user, it has to be displayed on the list view, irrespective of whether a user taps on it.

b. I have to indicate the number of notifications that user has received on the app icon. For instance, if you get a message in whats app, it displays the number of messages on the app icon.

Could anybody let me know what is the best possible way to implement these functionality? If anybody could point me to tutorials/references, it would be very helpful.

Thanks!

回答1:

a. Launch an activity from the receiver(GCM/C2DM receiver) when the push notification arrives

    // Your C2DM receiver (for GCM check the Android Documentation)
    public void onReceive(final Context context, final Intent intent)
    {
            if (intent.getAction().equals(com.google.android.c2dm.intent.RECEIVE))
            {
               //start your list view activity of notifications
               Intent i = new Intent();
               i.setClassName("com.test", "com.test.NotificationsActivity");
               i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               context.startActivity(i);
            }
    }

b. Check this.