2015-10-16 101 views
0

我得到Android應用程式,其接收推送消息在點擊後的推送通知的內容。訪問的推送消息(機器人)

如果用戶點擊推送消息,活動GoToNotification被調用。 這工作到目前爲止。

但我怎麼可以訪問推送消息的內容? Bundle.extra? 我想,對推送消息的部分內容作出反應,但捆綁額外總是空。

這裏我到目前爲止的代碼:

private void sendNotification(String msg, Bundle extras) { 
    mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      new Intent(this, GoToNotification.class), 0); 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.icon) 
        .setContentTitle("Warning") 
        .setStyle(new NotificationCompat.BigTextStyle() 
          .bigText(msg)) 
        .setAutoCancel(true) 
        .setWhen(System.currentTimeMillis()) 
        .setExtras(extras) 
        .setContentText(msg); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
} 


public class GoToNotification extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Bundle extras = this.getIntent().getExtras(); 
     if (extras != null) { 
      System.out.println(extras.toString()); 
     } else { 
      System.out.println("extras is null"); 
     } 
    } 
} 
+0

在烏爾sendNotification的()方法ü應該添加烏爾數據捆綁。 –

+0

pleae爲您提供GCM基地意向服務類的onMessage()方法的代碼 – warlock

回答

1
Intent notificationIntent = new Intent(context, SampleActivity.class); 
notificationIntent.putExtra("EXTRA_ID", "ur data"); 

和UR活動

if (extras != null) { 
ID = extras.getString("EXTRA_ID"); 
} 
0

謝謝,我設法解決這個問題,這裏是代碼:

由於已經寫好了,我需要把額外的內容。 同樣重要的是設定的requestId,它不是沒有它的工作。

下面是代碼工作:

private void sendNotification(String msg, String dpId) { 
    mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent intent = new Intent(this, GoToNotification.class); 
    intent.putExtra("dpId", dpId); 

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
    int requestID = (int) System.currentTimeMillis(); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, 
      intent, 0); 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.icon) 
        .setContentTitle("Test") 
        .setStyle(new NotificationCompat.BigTextStyle() 
          .bigText(msg)) 
        .setAutoCancel(true) 
        .setWhen(System.currentTimeMillis()) 
        .setContentText(msg); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(Integer.parseInt(dpId), mBuilder.build()); 
} 

public class GoToNotification extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); String regid; 

     Intent intent = getIntent(); 
     if (intent != null) { 
      if (intent.hasExtra("dpId")) { 
       System.out.println("Klick auf push dpid:" + intent.getStringExtra("dpId")); 

      } 
     } 
    } 
} 

清單:

<activity android:name=".GoToNotification" android:label="@string/app_name" />