2017-06-15 68 views
-1

我已經經歷了很多帖子,回合無法弄清楚有沒有辦法在應用程序退出時獲取推送通知。iOS - 獲取推送通知當應用程序強制退出用戶

我試着用content_available(true/1)和Priority爲High,我聽到通知聲音,但沒有通知徽章或App上的內容。任何線索將不勝感激。

{ to=/topics/lshekhar,          
    content_available=1,         
    collapse_key=sample, 
    delay_while_idle=true, 
    delivery_receipt_requested=true, 
    priority=10, 
    data={message={ "id" : "eARMS", 
        "submitter" : "lshekhar", 
        "topic" : "/topics/lshekhar" 
       }}, 
    time_to_live=10000, 
    notification={"sound":"default"}, 
    message_id=m-3319428685310488470, 
    badge=12} 
+0

它發生在前景或背景 –

+0

APNS我們不能在自動 –

+0

處理例如看到這個https://stackoverflow.com/questions/39382852/didreceiveremotenotification-not-called-ios-10/39383027#39383027 –

回答

1

這似乎是iOS 10中的問題。當您的有效載荷的主體密鑰爲空或「」(空字符串)時,會發生這種情況。

這可以很容易地與本地通知複製以及。要求您的APNS負載創建者將非空字符串添加到正文,並且通知將顯示橫幅。

"alert": { 
      "title": "Some title : ", 
      "body": "Some body text" 
} 

這應該可以解決您的問題。希望它可以幫助

編輯:

由於OP已要求訪問通知有效載荷的方式,當應用程序被退出,應用程序接收APNS,我更新了答案

您可以訪問APNS有效載荷,如果應用程序恰好收到APNS,而其退出使用的AppDelegate的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    if (launchOptions != nil) { 
     NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
     if (dictionary != nil) { 
      /*it is an APNS launch 
     } 
    } 

    ... 
} 

希望它可以幫助

+0

謝謝所以很多爲您的答案。 通過單擊通知它不會打開應用程序。此外,在didReceiveRemoteNotification方法中,將修改標題並保存SQL中的正文內容,這些內容將用於顯示收到的通知列表。任何想法,它在應用程序退出的情況下擊中哪種方法?這樣我可以自定義並保存在數據庫中? – leela

+0

我現在能夠以下面的格式獲得徽章。 {to =/topics/lshekhar.earms,content_available = true,collapse_key = sample,delay_while_idle = true,delivery_receipt_requested = true,priority = high, time_to_live = 10000, notification = {「sound」:「default」,「title 「:」 工具連接」, \t \t 「身體」:消息= { \t \t \t \t \t \t 「ID」: 「eARMS」, \t \t \t \t \t \t 「提交」: 「lshekhar」, \t \t \t \t \t \t「topic」:「/ topics/lshekhar。earms」, \t \t \t \t \t \t 「requestname」: 「的requestId - eARMS_2017-06-1515:24:20」 }, \t \t 「徽章」: 「12」},MESSAGE_ID = M-1317505031125464473} – leela

+0

@ leela:很高興我可以幫助:)如果應用程序退出並且您的應用程序碰巧收到通知,如果用戶點擊通知以打開應用程序,您的應用程序委託的didFinishLaunch with options方法的啓動選項字典將具有通知字典。它通過使用UIApplicationLaunchOptionsRemoteNotificationKey :) –