2013-10-24 144 views
2

通知點擊要求:我想通過點擊通知來處理(執行)IntentReceiver.java類,然後轉發意向。通知點擊處理推送通知有效載荷數據

IntentReceiver.java(廣播接收器的子類)---> Notification.java(活性)--->主儀表板(活性)

1.)在我的申請中,我有一個單獨的類「 IntentReceiver.java「,它是」BroadcastReceiver「的子類。 )然後,在我的「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> 

這是我需要的流量,能否請您提供做一個最好的辦法。 在此先感謝...

回答

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