Handle push notification payload data on notificat

2019-05-26 22:27发布

问题:

Requirement on notification clicked: I want to handle(execute) IntentReceiver.java class on click of the notification and then forward intent.

IntentReceiver.java(subclass of BroadcastReceiver) ---> Notification.java(activity) ---> Main dashboard(activity)

1.) In my application, I have a separate class "IntentReceiver.java", which is the subclass of "BroadcastReceiver".

2.) After that, inside my "IntentReceiver.java" class, I switch to the "Notification.java" class which have no layout screen, manipulate some data and switch to the main dashboard.

3.) On this main dashboard, I will handle different dialogue on behalf of the different keys received(via putExtra()) on main dashboard from "Notification .java" class after a manipulation.

Code for IntentReceiver.java class : This is a separate class to handle each of the notification.

public class IntentReceiver extends BroadcastReceiver {
        Context ctx;

        private static String PUSH_KEY_ALERT = "alert";

   @Override

        public void onReceive(Context context, Intent intent) {

            this.ctx = context;

        String alert = intent.getStringExtra(PUSH_KEY_ALERT);

        Bundle extras = getResultExtras(true);

        extras.putInt(PushIOManager.PUSH_STATUS, PushIOManager.PUSH_HANDLED_NOTIFICATION);

        setResultExtras(extras);

    }

}

Manifest Configuration:

<receiver android:name="com.DxS.android.push.IntentReceiver" > </receiver>



<activity android:name=".Notification">

        <action android:name="com.DxS.android.NOTIFICATIONPRESSED" />

        <category android:name="android.intent.category.DEFAULT" />

 </activity>



 <activity android:name=".dashboard"> </activity>

This is the flow of my requirement, Can you please provide a best way to do that. Thanks in advance...

回答1:

First of all, your onReceive method should check for errors. The following code will display a notification and start your Notification activity when the notification is tapped. I'm not sure what's the purpose of the Notification activity if it has no layout. Perhaps it doesn't have to be an activity, and tapping the notification should start the dashboard activity directly.

public class IntentReceiver extends BroadcastReceiver {
   static final String TAG = "IntentReceiver";
   Context ctx;

   @Override
   public void onReceive(Context context, Intent intent) {
       GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
       ctx = context;
       String messageType = gcm.getMessageType(intent);
       if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) 
           Log.i(TAG, "PUSH RECEIVED WITH ERROR: " + intent.getExtras().toString());
       else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) 
           Log.i(TAG, "DELETED PUSH MESSAGE: " + intent.getExtras().toString());
       else
       {
           Log.i(TAG, "Received PUSH: " + intent.getExtras().toString());
           postNotification(intent.getExtras());
       }
       setResultCode(Activity.RESULT_OK);
   }

  // post GCM message to notification center.
  private void postNotification(Bundle data) {
     String msg = data.getString("alert");
     Log.i(TAG, "message: " + msg);

     Intent intent = new Intent(ctx, Notification.class);
     PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, 0);

     NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
     .setContentTitle("Your Title")
     .setContentText(msg)
     .setTicker(msg)
     .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
     .setAutoCancel(true)
     .setOnlyAlertOnce(true)
     .setDefaults(Notification.DEFAULT_VIBRATE); 

     builder.setContentIntent(contentIntent);
     NotificationManager notificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
     notificationManager.notify(0, builder.build());
  }
}