Android push notification: Get data, store and display on new activity on click of notification

前端 未结 3 1611
孤街浪徒
孤街浪徒 2020-12-13 05:14

I am developing an application which is having push notification functionality. I followed the following link as Android Push Notification

I tried and successfully s

3条回答
  •  天涯浪人
    2020-12-13 06:00

    Send JSON data via push notification

    You can send the JSON as data in the notification message from your server side code. Once you get the notification then you would receive a JSON in the message where you can do whatever you want.

    Save the data into SQLite database

    This is simple as per your requirement, you can insert the data whatever received in the JSON. You can get the data from the JSON after parsing.

    Open new activity on click of push notification.

    You can do like below

    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);
    
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, YourActivity.class), 0);
    
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_stat_gcm)
    .setContentTitle("GCM Notification")
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(msg))
    .setContentText(msg);
    
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    

    Display data coming from push notification of new activity.

    You can display the data whatever receive from push message but you have to parse the JSON.

    If the application is closed so after click on notification the app get started.

    My above code will work for you in this case also.

    See here for JSON parsing : http://www.vogella.com/tutorials/AndroidJSON/article.html

    All in all, you have to add the data in the JSON form in your server cod that you would get when you push the GCM from the server and later perform parse the JSON and do whatever you want.

提交回复
热议问题