簡體   English   中英

iOS應用程序在后台時的GCM推送通知

[英]GCM push notification when iOS app is in the background

我正在嘗試使用GCM向我的iOS應用發送推送通知。 應用程序在后台處理時不會收到通知,但是當它在前台時會發出通知。 我正在使用PHP腳本測試推送通知,該腳本還將消息直接發送到APNS,並且它在后台運行。

發送給GCM的JSON :(我從其他客戶端發送它進行測試)

{
  "to" : "token...",
  "notification" : {
    "title": "GCM TITLE",
    "body" : "FROM GCM",
    "badge": "1",
    "sound": "default"
  }
}

不工作 :在didReceiveRemoteNotification中從GCM收到的userInfo:

Notification received: [aps: {
    alert =     {
        body = "FROM GCM";
        title = "GCM TILE";
    };
    badge = 1;
    sound = default;
}, gcm.message_id: 123...]

工作 :從PHP腳本發送時收到的userInfo(我還將message_id添加到JSON以查看是否存在問題)

Notification received: [aps: {
    alert =     {
        body = "FROM PHP";
        title = "PHP TITLE";
    };
    badge = 2;
    sound = default;
}, gcm.message_id: 123...]

我嘗試使用不同的組合將content_available添加到JSON但沒有幫助,還設置了Content-Type和Authorization請求標頭:

Content-Type:application/json
Authorization:key=... 

如果有人有同樣的問題,解決方案是我為JSON添加“優先級”:“高”。 這樣我就可以在后台收到通知。

{
  "to" : "token...",
  "priority": "high",
  "notification" : {
    "title": "GCM TITLE",
    "body" : "FROM GCM",
    "badge": "1",
    "sound": "default"
  }
}

要在app處於后台時收到通知,請注意我們需要添加:

"content_available":true // when app in background
"priority":"high" //when app is completely closed not even running in background

 // "content_available":true  ..most important field 

{
"to" : "token...",
 "priority":"high",
 "content_available":true,
 "notification" : {
 "title": "GCM TITLE",
 "body" : "FROM GCM",
 "badge": "1",
 "sound": "default"
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM