2016-07-07 91 views
0

問題是我的GCM消息到達我的應用程序,但沒有(自動)顯示在屏幕上。應該通過Android顯示到達的GCM消息嗎?

07-06 21:33:11.525 11269-11269/com.example.myapp D/ActivityThread: BDC-Calling onReceive: intent=Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.example.myapp cmp=com.example.myapp/com.google.android.gms.gcm.GcmReceiver (has extras) }, [email protected] 
07-06 21:33:11.534 11269-11269/com.example.myapp D/ActivityThread: BDC-RECEIVER handled : 0/ReceiverData{intent=Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.example.myapp (has extras) } packageName=com.example.myapp resultCode=-1 resultData=null resultExtras=null} 
07-06 21:33:11.539 11269-11269/com.example.myapp D/ActivityThread: SVC-Calling onStartCommand: [email protected], flags=0, startId=1 
07-06 21:33:11.539 11269-11269/com.example.myapp D/ActivityThread: SVC-Creating service: CreateServiceData{[email protected] className=com.example.myapp.MyGcmListenerService packageName=com.example.myapp intent=null} 
07-06 21:33:11.539 11269-11269/com.example.myapp D/ActivityThread: SVC-CREATE_SERVICE handled : 0/CreateServiceData{[email protected] className=com.example.myapp.MyGcmListenerService packageName=com.example.myapp intent=null} 
07-06 21:33:11.543 11269-11269/com.example.myapp D/ActivityThread: SVC-SERVICE_ARGS handled : 0/ServiceArgsData{[email protected] startId=1 args=Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.example.myapp (has extras) }}07-06 21:33:11.565 11269-11269/com.example.myapp D/ActivityThread: SVC-Destroying service: [email protected] 
07-06 21:33:11.565 11269-11269/com.example.myapp D/ActivityThread: SVC-STOP_SERVICE handled : 0/[email protected] 
07-07 21:33:11.564 11269-11530/com.example.myapp D/MyGcmListenerService: bundle: Bundle[{key1=message1, key2=message2, notification=Bundle[{e=1, body=This is a notification that will be displayed ASAP., icon=ic_launcher, title=Hello, World}], collapse_key=com.example.myapp}] 
07-06 21:33:11.564 11269-11530/com.example.myapp D/MyGcmListenerService: From: 28REDACTED98 
07-06 21:33:11.564 11269-11530/com.example.myapp D/MyGcmListenerService: Message: null 

我接收的代碼是:

public class MyGcmListenerService extends GcmListenerService { 
    public static final String TAG = MyGcmListenerService.class.getSimpleName(); 

    @Override 
    public void onMessageReceived(String from, Bundle data) { 
     Log.d(TAG, "bundle: " + data); 
     String message = data.getString("message"); 
     Log.d(TAG, "From: " + from); 
     Log.d(TAG, "Message: " + message); 
     ... 

發送代碼(使用節點-GCM)是:

var message = new ngcm.Message({ 
     priority: 'high', 
     contentAvailable: true, 
     restrictedPackageName: "com.example.myapp", 
     data: { 
       key1: 'message1', 
       key2: 'message2' 
     }, 
     notification: { 
       title: "Hello, World", 
       icon: "ic_launcher", 
       body: "This is a notification that will be displayed ASAP." 
     } 
    }); 

我已經有和沒有contentAvailablerestrictedPackage嘗試,但他們並不區別。

我的問題是:

  1. 如果我接收的代碼需要在消息到達創建屏幕上的通知,還是應該這(我相信)是對我由Android做了什麼?
  2. 示例代碼說該捆綁應該包含一個data.getString("message");,但我的應用程序將其視爲null。爲什麼?
+1

要獲得自動創建的通知,你的推送消息必須遵循特定的消息格式,否則你必須創建它手動 –

+0

@VivekMishra感謝您的信息。你能用一個具體格式的例子來寫答案嗎? – fadedbee

回答

1

要顯示GCM通知會自動將您推送消息的JSON格式應該是相同的這樣

"notification" : { 
      "body" : "great match!", 
      "icon" : "ic_launcher.png", 
      "title" : "Portugal vs. Denmark" 
       } 

爲了進一步澄清,你可以看到這個鏈接
Android GCM Notifications

+0

非常感謝您的回答。你能解釋一下你的例子與我發送的消息中存在的'notification'有什麼不同嗎?在接收到的bundle'Bundle [{key1 = message1,key2 = message2,notification = Bundle [{e = 1,body = this是一個將盡快顯示的通知,icon = ic_launcher,title = Hello,World}],collapse_key = com.example.myapp}]'? – fadedbee

+1

@chrisdew唯一的區別是我可以在你的包中看到額外的'e = 1'。 –

+1

@chrisdew我在回答中添加了一個供參考的鏈接 –

相關問題