iOS - 当应用程序被用户强制退出时获取推送通知

iOS - Get Push Notifications When App Is Force-Quit By User

我浏览了很多帖子,但还是不清楚有没有办法在应用程序退出时获得推送通知。

我尝试使用 content_available 和 (true/1) 并且优先级为高,我听到了通知声音,但听不到应用程序上的通知徽章或内容。任何线索将不胜感激。

{ 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}

这似乎是 iOS10 中的问题。当您的有效载荷的正文键为空或“”(空字符串)时会发生这种情况。

这也可以通过本地通知轻松复制。要求您的 APNS 负载创建者将非空字符串添加到正文,通知将显示横幅。

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

这应该可以解决您的问题。希望对你有帮助

编辑:

由于OP已经询问了当应用程序退出并且应用程序收到APNS时访问通知负载的方式,我正在更新答案

如果应用程序在使用 AppDelegate 退出时碰巧收到 APNS,您可以访问 APNS 负载

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

   ...
}

希望对您有所帮助