Reputation: 1974
I'm trying to get my remote notifications to work properly, but I've encountered some problems.
I'm sending notifications from my server with the content-available
flag set to 1
, such that my didReceiveRemoteNotification
is being triggered, and I display the notification to the user by triggering the following method inside didReceiveRemoteNotification
:
- (void) showPush:(NSString *)message
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.body = message;
content.sound = [UNNotificationSound defaultSound];
NSString *identifier = @"UYLLocalNotification";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
content:content trigger:nil];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@",error);
}
}];
}
Notifications being sent while the app is in the suspended state are not displayed at all, but are displayed after the app has been opened and then closed again, like this:
-> App is suspended: notification is sent
-> App is opened and in the foreground: I can see that the notification has been processed
-> App is closed and in the background: notification is being displayed with a banner
Any suggestions to why this does not work as intended would be appreciated.
Upvotes: 1
Views: 68
Reputation: 21244
Notifications can either be user-facing (alert, badge, or sound) or silent (content-available). They can not be both.
When a notification contains both user-facing elements and the content-available flag indicating a silent push APNS and the device are put into an undefined state.
Upvotes: 0
Reputation: 1094
I think you need to call showPush
from a slightly different app delegate call back:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
The docs for this call back note:
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.
Upvotes: 1