2

我的問題可能是壞的,但我找不到任何答案的任何地方,我迷路了......在iOS中豐富的推送通知使用數據與FCM


所以我想顯示了豐富的通知在iOS 10+中擁有不錯的圖像。

爲此,我使用FCM和UNNotificationServiceExtension,如果我理解正確,應該獲取數據有效負載,找到圖像URL並在修改UNNotificationContent之前加載它。

我遇到的問題是我無法掌握這個「數據」。

什麼我發送給FCM如下:

{ 
"to": "device_token", 
"content_available": true, 
"mutable_content": true, 
"badge" : 9, 
"notification": { 
    "title": "You still need the iPhone ?", 
    "body": "Give it back if you finished you tests !" 
}, 
"data": { 
    "message": "test !", 
    "mediaUrl": "http://usr.audioasylum.com/images/2/20352/Cat_and_rose.jpg" 
}, 
"priority": "high" 
} 

我得到的電話是:

{ 
aps =  { 
    alert =   { 
     body = "Give it back if you finished you tests !"; 
     title = "You still need the iPhone ?"; 
    }; 
    "content-available" = 1; 
    "mutable-content" = 1; 
}; 
"gcm.message_id" = "0:1489054783357873%1ee659bb1ee659bb"; 
mediaUrl = "http://usr.audioasylum.com/images/2/20352/Cat_and_rose.jpg"; 
message = "Offer!"; 
} 

據我瞭解,在mediaURL和消息應該結束了「aps」,而不是外部,這就是爲什麼我無法在擴展中找到它們的原因。

和擴展我進去:(我把它分解上昏迷了更多可讀性)

<UNNotificationRequest: 0x117d3c170; 
identifier: FDC13B60-FE5A-40C6-896D-06D422043CCE 
content: <UNNotificationContent: 0x117d3a650; title: You still need the iPhone ? 
subtitle: (null) 
body: Give it back if you finished you tests ! 
categoryIdentifier: 
launchImageName: 
peopleIdentifiers:() 
threadIdentifier: 
attachments:() 
badge: (null) 
sound: (null) 
hasDefaultAction: YES 
shouldAddToNotificationsList: YES 
shouldAlwaysAlertWhileAppIsForeground: NO 
shouldLockDevice: NO 
shouldPauseMedia: NO 
isSnoozeable: NO 
fromSnooze: NO 
darwinNotificationName: (null) 
darwinSnoozedNotificationName: (null) 
trigger: <UNPushNotificationTrigger: 0x117d3fe90; contentAvailable: YES 
mutableContent: YES>> 

是這個問題我怎麼發信息與FCM,或者我找回它們的方式嗎?也許這是我對待他們的方式?

一如既往,感謝您的幫助!

編輯:爲接收器添加的代碼(這只是在擴展打印)

@interface NotificationService() 

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver); 
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent; 

@end 

@implementation NotificationService 

- (void) didReceiveNotificationRequest: (UNNotificationRequest *) request 
        withContentHandler: (void (^)(UNNotificationContent *_Nonnull))contentHandler 
{ 
    self.contentHandler = contentHandler; 
    self.bestAttemptContent = [request.content mutableCopy]; 

    NSLog(@"------------------- %@ -------------------", request); 

    // Modify the notification content here... 
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title]; 
    self.bestAttemptContent.body = [NSString stringWithFormat:@"%@", request.content]; 
    self.contentHandler(self.bestAttemptContent); 
} 
+0

嗨。如果您可以發佈您的代碼片段來接收郵件,我認爲這將有助於社區。 :) –

+1

完成,但它確實只是代碼示例中的一個打印:) – SeikoTheWiz

+0

嗨@SeikoTheWiz對不起,我在這裏沒有太多的幫助。我也在我的答案[這裏](http://stackoverflow.com/a/42263795/4625829)中看到了你的評論(與這篇文章有關嗎?)。我會嘗試環顧四周,看看我在這裏找到了什麼(但不能承諾任何)。有一件事引起了我的注意,儘管目前爲止,'badge'參數應該在'notification'中。無論如何,你希望解釋「數據」有效載荷的行爲是怎樣的,對吧?爲什麼數據出現在「aps」之外? –

回答

1

我認爲你應該檢查self.bestAttemptContent?.userInfomediaUrl數據。

+0

這就是它!我很確定我測試了它,但似乎我沒有-_- 非常感謝! – SeikoTheWiz

1

我看到這裏有兩個主要問題:

我的問題是,我無法掌握這個「數據」。

你提到你期待的是,mediaUrl參數應該是裏面aps。 (mediaUrlaps之外)。要獲取詳細信息,你就必須實現什麼樣的documentation指出:

iOS上10接收數據消息

要接收數據的消息時,您的應用程序是在前臺,在iOS 10設備,你需要處理applicationReceivedRemoteMessage :.您的應用程序可以仍然收到數據消息時,它是沒有這個回調的背景,但對前景的情況下,你需要像你的應用程序代理以下邏輯:

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
// Receive data message on iOS 10 devices while app is in the foreground. 
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage { 
    // Print full message 
    NSLog(@"%@", remoteMessage.appData); 
} 
#endif 

附::我不完全確定你是否已經嘗試了這一點,但在你的文章中沒有任何類似的代碼,所以我認爲這應該嘗試。

據我瞭解,在mediaURL和消息應該在「APS」結束了,而不是外面,這就是爲什麼我不能在擴展中找到它們。

的一點是,只有specific parameters是內部aps可以預料:

aps字典中的任何其他按鍵被蘋果忽略。

與此同時,在FCM中只能使用一些corresponding parameters,它將出現在aps負載中。因此,您應該簡單地期望您包含的任何自定義鍵值對都將超出aps參數。從Apple文檔以上(重點煤礦):

除了aps字典,的JSON字典可以包括自定義鍵和值與您的應用程序特定的內容

關於blog關於您在評論中鏈接的內容,我沒有深入研究它,因爲它沒有真正使用FCM,最好假設行爲是不同的。

+0

我花了一段時間纔回到這裏,並用新鮮的眼睛*設法解決了這個問題。我希望我確實得到了你需要的一點。該文檔通常具有您[iOS中的接收郵件](https://firebase.google.com/docs/cloud-messaging/ios/receive)所需的內容。 –

+0

謝謝你的回答! 問題是當我想在應用程序未運行時推送通知時。修改發生的事情的唯一方法是運行UNNotificationServiceExtension,然後修改通知並讓系統顯示它。在這種情況下不會調用applicationReceivedRemoteMessage。或者,也許我做錯了什麼,擴展應該喚醒應用程序,然後通過正常的過程? – SeikoTheWiz