处理通知点击时的推送通知有效载荷数据

时间:2013-10-24 12:38:53

标签: android push-notification

点击通知要求:我想点击通知处理(执行)IntentReceiver.java类,然后转发意图。

IntentReceiver.java(BroadcastReceiver的子类)---> Notification.java(activity)--->主仪表板(活动)

1。)在我的应用程序中,我有一个单独的类“IntentReceiver.java”,它是“BroadcastReceiver”的子类。

2.)之后,在我的“IntentReceiver.java”类中,我切换到“Notification.java”类,它没有     布局屏幕,操纵一些数据并切换到主仪表板。

3.。)在这个主仪表板上,我将代表收到的不同密钥处理不同的对话(通过putExtra())      在操作之后来自“Notification .java”类的主仪表板上。

IntentReceiver.java类的代码:这是一个单独的类来处理每个通知。

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

    }

}

清单配置:

<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>

这是我的要求流程,请您提供最好的方法。 提前谢谢......

1 个答案:

答案 0 :(得分:2)

首先,您的onReceive方法应检查错误。 以下代码将显示通知,并在点按通知时启动您的Notification活动。如果它没有布局,我不确定Notification活动的目的是什么。也许它不一定是活动,点击通知应直接启动dashboard活动。

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