1

我使用Firebase雲端函數通過Cloud Messaging向iOS設備發送通知。 sendToDevice方法工作良好,並且返回成功承諾,但Firebase函數日誌中沒有任何錯誤或警告,但在iOS上不顯示通知。如果我通過Firebase通知信息中心發送通知,該通知很有用,並且會在設備上顯示通知。我不知道我在哪裏可以有bug。這個錯誤可能在iOS應用程序端,即使它像我說的那樣工作?成功sendToDevice後Firebase雲消息傳遞不起作用

火力地堡配置:

var functions = require('firebase-functions'); 
var admin = require('firebase-admin'); 
var config = functions.config(); 
admin.initializeApp(functions.config().firebase); 

雲功能的部分:

APP.services.firebase.admin.database().ref('tokens/' + userId).once('value', function(snapshot) { 

    var userDevicesTokens = []; 

    snapshot.forEach(function(childSnapshot) { 
     userDevicesTokens.push(childSnapshot.key) 
    }); 

    if (userDevicesTokens.length === 0) { 
     console.warn('No tokens for user'); 
     return; 
    } 

    var payload = { 
     data: { 
      title: options.title, 
      text: options.text, 
      type: options.type, 
     } 
    }; 
    APP.services.firebase.admin.messaging().sendToDevice(userDevicesTokens, payload) 
     .then(function(response) { 

      console.log("Successfully sent message:", response); 

     }) 
     .catch(function(error) { 
      console.log("Error sending message:", error); 
     }); 

}) 

火力地堡雲功能登錄:

11: 52: 43.952 AM info newChatMessageTrigger 
Successfully sent message: { 
    results: [{ 
     messageId: '0:15016....' 
    }], 
    canonicalRegistrationTokenCount: 0, 
    failureCount: 0, 
    successCount: 1, 
    multicastId: 589.... 
} 

11: 52: 22.760 AM outlined_flag newChatMessageTrigger 
Function execution took 604 ms, finished with status: 'ok' 

11: 52: 22.252 AM outlined_flag newChatMessageTrigger 
Billing account not configured.External network is not accessible and quotas are severely limited.Configure billing account to remove these restrictions 

11: 52: 22.252 AM outlined_flag newChatMessageTrigger 
Function execution started 
+0

您是否嘗試發送'notification'消息有效呢? –

回答

2

我認爲這是與你的有效載荷的問題。

用您的Firebase功能嘗試一次這個有效載荷,如果您在iOS上收到通知,請告訴我。

var payload = { 
     data: { 
      title: "Welcome to ChitChat Group", 
      message: "You may have new messages" 
     } 
    }; 
+0

它仍然不起作用。相同的日誌像早些時候 – Damian

+0

檢查我的答案[這裏](https://stackoverflow.com/questions/45428888/how-do-i-give-notification-every-time-user-get-registered-to-my-app-使用雲/ 45429100?noredirect = 1個#comment77819438_45429100)。考慮到用戶只有一個設備令牌,我已收到通知iOS的此雲功能。 – Surjeet

+0

單個令牌不起作用。 – Damian

0

我寫信給Firebase支持,問題與消息類型有關。在Firebase中,我們有兩種類型的消息:數據和通知。在這種情況下,我們應該使用類型通知(see documentation)。

有效載荷應該是這樣的:

var payload = { 
    notification: { 
     title: options.title, 
     body: options.text, 
    }, 
    data: { 
     type: options.type, 
    } 
}; 
相關問題