问题
I am banging my head. I am implementing push notification. Everything is working fine (push is received, badge is updated) but under iOS 13.3 the method application(_:didReceiveRemoteNotification:fetchCompletionHandler:) is not called when the app is in the background. If the app is in the foreground or using an iOS 12 device the method is called. I register for push notification in the following way:
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
}];
The payload is set to the following
{"aps": {
"badge": 10,
"alert": "test",
"content-available": 1
}}
I tried adding "Remote notifications" and "Background processing" as app capabilities in all variations (only "Remote notifications"/"Background processing", without any of those capabilities, enabling both) without any change. I set the delegate for the UNUserNotificationCenter but again without success. I set the headers accordingly:
curl -v \
-H 'apns-priority: 4' \
-H 'apns-topic: xx.xxxxx.xxxx' \
-H 'apns-push-type: alert' \
-H 'Content-Type: application/json; charset=utf-8' \
-d '{"aps": {"badge": 10,"alert": "test", "content-available":1}}' \
--http2 \
--cert pushcert.pem \
https://api.sandbox.push.apple.com/3/device/1234567890
From the docs it states that this method is called even when the app is in background:
Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background.
What am I missing here for iOS 13?
回答1:
Have you set
"content-available": 1
in you backend APS payload?
Also need to make sure you have enabled background mode in your iOS App's info.plist file
<key>UIBackgroundModes</key>
<array>
<string>processing</string>
<string>remote-notification</string>
</array>
回答2:
I spend a support ticket to get an answer to this problem.
It turns out that the documentation is not 100% "valid" for iOS 13 on this topic. The device decides whether to wake up or not. Although the documentation states a little different.
Apple's preferred way of implementation as a notification extension. After that, you have to adapt the payload to include "mutable-content".
I asked the support afterward if I should file a radar and they replied with "yes".
回答3:
This delegate method : -
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
is getting invoked when we click on notification for iOS 13 and app is in background.
回答4:
Implement didRegisterForRemoteNotificationsWithDeviceToken
and also didFailToRegisterForRemoteNotificationsWithError
in your app's delegate, to check if the device makes a good connection with Apple's APN server. If this is not the case, restart the device and/or try to make a connection via another Wi-Fi network and restart the app.
回答5:
Need activated "BackGround fetch" go to File Project - signIn capabilities - BackgroundModes - and check Background fetch and Remote notifications as well
remember in didFinishAlunchWithOptions
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
with that I have a response in method - didReceive response - in background but only if tap the notification.
I found this info... but i dont know if is real
"If this is a new system behavior from iOS, then it's unlikely that FCM will be able to provide a workaround. A few things to note:
Apple's docs mention that silent notifications will be throttled by the system to conserve power. In the past, we've seen that testing silent notifications with your device plugged in to a power source will reduce throttling. In older iOS versions, Apple's docs have mentioned that force-quitting an application will prevent wakeups from silent notifications entirely. I'm not sure if this is still the case."
I Still looking around for the notifications in background for update badge icon in the app, at the momen I receive the notification.
回答6:
I had the same problem. Read this :https://medium.com/fenrir-inc/handling-ios-push-notifications-the-not-so-apparent-side-420891ddf10b
I use Iphone XS Software Version 13.7
1. Before iOS 10, using the UIApplication method: registerUserNotificationSettings(:) iOS 10 onwards, using the UserNotifications framework methods: requestAuthorization(options:completionHandler:) setNotificationCategories(:)
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
(granted, error) => InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications));
}
else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
else
{
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
}
“Push Notifications”, "Remote notifications" and Background Modes capability is required (info.plist and Entitlements.plist)
3.
Implement DidReceiveRemoteNotification method and RegisteredForRemoteNotifications
After this change the DidReceiveRemoteNotification method was called.
Try this to silent Push Notification:
{
"aps" : {
"alert" : "",
"content-available" : 1
},
}
Try this to Normal Push Notification:
{
"aps" : {
"alert" : "Alert!!!"
},
}
回答7:
You need to implement a Notification Content Extension
As i was using OneSignal and it's setup code, this worked fine for me https://documentation.onesignal.com/docs/ios-sdk-setup
not sure if the OneSignal bits make a difference, but appending them anyway
import UserNotifications
import OneSignal
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var receivedRequest: UNNotificationRequest!
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.receivedRequest = request;
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
OneSignal.didReceiveNotificationExtensionRequest(self.receivedRequest, with: self.bestAttemptContent)
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
OneSignal.serviceExtensionTimeWillExpireRequest(self.receivedRequest, with: self.bestAttemptContent)
contentHandler(bestAttemptContent)
}
}
}
来源:https://stackoverflow.com/questions/59876964/didreceiveremotenotification-not-called-in-ios-13-3-when-app-is-in-background