2

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

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

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

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

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

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>

这是我的要求流程,请您提供一个最好的方法来做到这一点。提前致谢...

4

1 回答 1

2

首先,您的onReceive方法应该检查错误。以下代码将显示通知并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());
  }
}
于 2013-10-24T15:25:43.213 回答