iOS 10 : Receive Remote Notification in background
I finally got this fixed after struggle 2 months by adding 2 things :
This is missing in all documentation and is to help all whom I wish not to struggle like me
|*| Try 1) : For Ios 10 background notification, Add following deligate method in AppDeligate :
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings)
{
application.registerForRemoteNotifications()
}
Got this solution from : https://codentrick.com/firebase-cloud-messaging-ios/
|*| Try 2) : If using Firebase : Set a value in "info.plist"
Property List View :
FirebaseAppDelegateProxyEnabled -> NO
Source Code View :
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
I Had only mentioned FirebaseAppDelegateProxyEnable. So be careful of spelling too.
Background fetch execution time with iOS 10 UserNotifications without user interaction
I found a solution that doesn't work directly with the UserNotifications
framework but still allows you to perform background jobs in iOS 10 via push notifications.
I found that by using the UIApplicationDelegate
you can in fact perform background jobs via push notifications through the same function used in previous iOS versions:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler{
//execute the background job here
}
All you have to do is add the content-available
flag to the push before sending it and set it equal to 1.
Like so for js:
data: {
alert: "Some alert",
type: someType,
"content-available": 1,
id: someId
}
Push notification issue with iOS 10
For iOS 10 using xCode 8 GM.
I have resolved my issue with following steps using xCode 8 GM for iOS 10:
1) In the targets, under Capabilities enable Push Notifications to add Push Notifications Entitlements.
2) Implement UserNotifications.framework into your app. Import UserNotifications.framework in your AppDelegate.
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end
3) In didFinishLaunchingWithOptions method assign UIUserNotificationSettings
and implement UNUserNotificationCenter
delegate.
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if( !error ){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
}
return YES;
}
4) Now finally implement this two delegate methods.
//============For iOS 10=============
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
//Called when a notification is delivered to a foreground app.
NSLog(@"Userinfo %@",notification.request.content.userInfo);
completionHandler(UNNotificationPresentationOptionAlert);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
//Called to let your app know which action was selected by the user for a given notification.
NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
}
Please remain the code as it is you are using for iOS 9,
Only add lines of code to support Push notification for iOS 10 using UserNotifications.framework.
Get push notification while App in foreground iOS
If the application is running in the foreground, iOS won't show a notification banner/alert. That's by design. But we can achieve it by using UILocalNotification
as follows
Check whether application is in active state on receiving a remote
notification. If in active state fire a UILocalNotification.if (application.applicationState == UIApplicationStateActive ) {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = message;
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
SWIFT:
if application.applicationState == .active {
var localNotification = UILocalNotification()
localNotification.userInfo = userInfo
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.alertBody = message
localNotification.fireDate = Date()
UIApplication.shared.scheduleLocalNotification(localNotification)
}
Related Topics
How to Create a Development Framework in iOS Including Swift
How to Know Users Click Fast Forward and Fast Rewind Buttons on the Playback Controls in Iphone
Why Can't I Call the Default Super.Init() on Uiviewcontroller in Swift
Class Plbuildversion Is Implemented in Both/Applications
How to Configure Full Urls in Xcconfig Files
Mfmailcomposeviewcontroller in iOS 7 Statusbar Are Black
Xcode 8 Custom Font Doesn't Show Up in Interface Builder
Xcode Cannot Run on the Selected Destination
Dynamically Increase Height of Uilabel & Tableview Cell
Uiwebview and Safari Comparison
Storing Uicolor Object in Core Data
Google Maps iOS Sdk: Custom Icons to Be Used as Markers
Changed +Load Method Order in Xcode 7
Hide Device Volume Hud View While Adjusitng Volume with Mpvolumeview Slider
Destinationviewcontroller Segue and Uinavigationcontroller Swift
Found an Unexpected MACh-O Header Code: 1918975009 in Xcode 6