2016-05-31 164 views
3

我在Android應用程序中實現了Google Cloud Messaging。當我打開應用程序時使用JSON發送消息時,它的行爲與關閉應用程序時不同。當應用程序在後臺時推送通知

當我有應用程序打開,並收到通知,它開始我希望它啓動的意圖。當我收到通知時,我的應用程序已關閉,並且我單擊通知,它會打開主要目的。我怎麼能說我打開應用程序並收到推送通知時需要打開哪個意圖?

在MyGcmListenerService代碼

public class MyGcmListenerService extends GcmListenerService { 

    private static final String TAG = "MyGcmListenerService"; 
    File fileDir, file; 
    String filename, filestring; 

    /** 
    * Called when message is received. 
    * 
    * @param from SenderID of the sender. 
    * @param data Data bundle containing message data as key/value pairs. 
    *    For Set of keys use data.keySet(). 
    */ 

    @Override 
    public void onMessageReceived(String from, Bundle data) { 

     Bundle notification = data.getBundle("notification"); 
     String title = notification.getString("title"); 
     String naam = notification.getString("naam"); 
     String nfcId = notification.getString("nfcId"); 
     Log.d(TAG, "From: " + from); 
     Log.d(TAG, "Title: " + title); 
     Log.d(TAG, "Naam: " + naam); 
     Log.d(TAG, "nfcId: " + nfcId); 

     if (from.startsWith("/topics/")) { 

     } else { 
      filename = "gegevensOverledene"; 
      ReadWriteFile readWriteFile = new ReadWriteFile(getApplicationContext()); 
      readWriteFile.writeFileOverledene(filename, naam); 
     } 

     sendNotification(title, naam, nfcId); 
    } 

    /** 
    * Create and show a simple notification containing the received GCM message. 
    */ 
    private void sendNotification(String title, String naam, String nfcId) { 
     Intent intent = new Intent(this, PinLoginActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.putExtra("naam", naam); 
     intent.putExtra("nfcId",nfcId); 
     intent.putExtra("Class",NieuweOverledeneActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); 
     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle(title) 
      .setContentText(naam) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

     NotificationManager notificationManager =     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 
} 

我送的JSON,請注意,我離開了token_mobile故意

{ 
    "to": "TOKEN_MOBILE****************", 
    "notification": { 
     "title": "Nieuwe overledene", 
     "body": "Henk", 
     "naam": "Henk", 
     "nfcId": "80-40-A3-4A-C2-D6-04" 
    } 
} 
+0

它是在你的sendNotification方法中,你需要設置正確的意圖到PendingIntent你開始 –

+0

無論我在sendNotification中設置了哪個意圖,當我在應用程序關閉時收到消息時,它**總是* *啓動主要意圖 – willemjan92

+1

這很奇怪 - 你可能在你的'PinLoginActivity'中做任何檢查,可能是將用戶重定向到主要活動?請向我們展示'PinLoginActivity'代碼。另外,嘗試將您的'意圖'標誌更改爲'intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);' – ishmaelMakitla

回答

-1

進行以下檢查

/** 
* Create and show a simple notification containing the received GCM message. 
*/ 
private void sendNotification(String title, String naam, String nfcId) { 

    //Create the intent according to your condition and pass it to pendingintent. 

    Intent intent = new Intent(this, PinLoginActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.putExtra("naam", naam); 
    intent.putExtra("nfcId",nfcId); 
    intent.putExtra("Class",NieuweOverledeneActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); 
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.mipmap.ic_launcher) 
     .setContentTitle(title) 
     .setContentText(naam) 
     .setAutoCancel(true) 
     .setSound(defaultSoundUri) 
     .setContentIntent(pendingIntent); 

    NotificationManager notificationManager =     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 
+0

當我在應用程序關閉時收到消息時,我在sendNotification中設置了哪個意圖並不重要,它**總是**開始主要意圖。只有在收到通知時打開應用程序時纔會調用sendNotification – willemjan92

3

您正在發送通知消息。詳細瞭解如何處理通知消息here

當您的應用程序處於前臺通知消息傳遞給您的onMessageReceived回調。在那裏你可以決定接收到的消息應該做什麼,例如:生成並顯示通知或與你的應用服務器或其他任何東西同步。

當你的應用是在後臺通知消息自動地生成基於在您的下游消息請求的「通知」對象傳遞的屬性通知,onMessageReceived未在此情況下調用。如果你看一下reference docs的通知消息,你會看到一個名爲場click_action你可以用這種方法來定義時自動生成的通知被竊聽推出什麼活動:

在Android上,如果設置,活動當用戶單擊通知時,將啓動匹配的意圖過濾器 。

如果未設置,則啓動主Activity,這是您現在看到的。

注意:此行爲僅適用於通知消息,如果只發送數據消息,則所有發送的消息將導致調用onMessageReceived

+0

如何處理帶有應用程序的通知消息在後臺? – Sanoop

+0

通知消息由應用程序在後臺導致顯示的通知時由客戶端庫處理。如果該通知消息具有伴隨的數據有效載荷,則由於用戶點擊通知而導致啓動的附加內容將可用。 –

+0

因此,基本上如果與通知相關的動作應該伴隨數據有效載荷或通知有效載荷。我的理解是否正確? – Sanoop

相關問題