iOS Push Notification - ios

I use APNS and it's work fine on iOS 9.
With the new push API changes on iOS10 i cant register for push notification so i insert the next changes:
Enable push notification in the target capabilities tab.
In didFinishLaunchingWithOptions we check the OS version and register as followed :
if (SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(#"10.0")) {
//ios 10 Notifiction
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if( !error ){
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog(#"iOS 10 push notification register successfully ");
}
}];
} else {
// iOS 8-9 Notifications
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog(#"iOS 9 push notification register successfully ");
}
With this changes I manage to perform registration in iOS 9 and iOS 10 but I have couple of problems :
Once I enable the push notification in the target capabilities tab the push notification stop working on iOS 9 although the registration was complete successfully .
The push doesn’t work on iOS 10 at all although the registration was complete successfully .
Please keep in mind that if I turn off the push notification in the target capabilities tab (with the same code) the push return to work on iOS 9 but I cant register for APNS on iOS 10.

if #available(iOS 8.0, *) {
let settings: UIUserNotificationSettings = UIUserNotificationSettings (types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}else {
let types: UIRemoteNotificationType = [.alert, .badge, .sound]
application.registerForRemoteNotifications(matching: types)
}

Related

Allow push notification popup not displayed

I have developed an app for both Android and IOS to receive push notifications, using phone gap push plugin. when I deploy app on Android device, I'm able to receive push notifications. But when I deploy app on IOS and run app for the first time. I'm not getting the popup "Allow push notification", which will give permission for the app to receive push notification. I want to know if anybody has faced this problem earlier or any ideas to fix this issue.
I followed several posts online, but couldn't find any thing related to this. As per my understand popup should be displayed by plugin by default.
Thanks in Advance.
If you use Objective-C, use below code:
// AppDelegate.m
#import UserNotifications;
After that, in method "didFinishLaunchingWithOptions" add this code:
if( SYSTEM_VERSION_LESS_THAN( #"10.0" ) )
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
if( optind != nil )
{
NSLog( #"registerForPushWithOptions:" );
}
}
else
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
{
if( !error )
{
NSLog( #"Push registration success." );
}
else
{
NSLog(#"Something went wrong");
}
}];
}
If you use swift, read this https://useyourloaf.com/blog/local-notifications-with-ios-10/ . I think it helpful for you.

Can I use UILocalNotifications and UNUserNotifications?

I have an iOS app that includes user 'alarms' - sent to user when the the app is not in foreground. I am using UNUserNotifications and all is working well in iOS 10 and iOS 11 testing.
I would also like to reach users who are still using iOS 8 and iOS 9.
In order to send notifications to the iOS 8 users, do I need to include alternate methods that use UILocalNotifications? Or will iOS 8 respond correctly UNUserNotificatons?
If I need to include both, I can use some if's to use the right one based on OS. It just seems odd that I must include a deprecated technique.
UNUserNotifications are iOS 10 and higher, so won't work on iOS 8 and iOS 9. In that case you should check if is UNUserNotifications exists, or otherwise fall back to the older methods, eg:
if (NSClassFromString(#"UNUserNotificationCenter")) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = (UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound);
[center requestAuthorizationWithOptions: options
completionHandler: ^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(#"Granted notifications!");
}
}];
}
else {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: userNotificationTypes categories: nil];
[[UIApplication sharedApplication] registerUserNotificationSettings: settings];
}
The UserNotifications framework was added to iOS with iOS 10, so for any version before that, you'll need to use the older UILocalNotification. You are correct that UILocalNotification was deprecated in iOS 10 in favor of UserNotifications.framework, but before iOS 10, symbols from the UserNotifications framework are unavailable, so there's no other way. You can use a simple iOS version check to determine when to use either method:
if(#available(iOS 10, *)){
//UserNotifications method
}
else{
//UILocalNotification method
}

How do I ask for notifications permission in an IBAction?

In my on-boarding I have a UIPageViewController containing a ‘primer’ screen at the end for authorizing notifications. The user would tap a button labeled “Enable Notifications” and the notifications permission dialog would appear. How do I accomplish this?
You can put:
Objective-C
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
[[UIApplication sharedApplication] registerForRemoteNotifications]; // you can also set here for local notification.
Swift
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
UIApplication.shared.registerForRemoteNotifications() // you can also set here for local notification.
inside your IBAction.
Please remember also add import UserNotifications for Swift or #import <UserNotifications/UserNotifications.h> for Objective-C in file where you have IBAction and make sure that Push Notification is activated under target - Capabilities - Push notification.
Objective-C:
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}

Check for framework availability at run time

To generate local notifications in my library, I need to use either UserNotification or UILocalNotification depending on what my host app uses. (Some customers are still using the deprecated didReceiveLocalNotification: API).
Now when I create my notification, is it possible, at runtime, to determine which system the host app uses and create the appropriate APIs. This means I will need to conditionally import and use the UserNotification header file.
EDIT:
Regarding the use of NSClassFromString:
if (NSClassFromString(#"FrameworkClass") == nil) {
// the framework is not available
} else {
// the framework is avaiable
}
But I have a lot of UserNotification code to write. I don't think using performSelector: would be very practical.
What about preprocessor macros?
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
// Only COMPILE this if compiled against BaseSDK iOS10.0 or greater
#import <UserNotifications/UserNotifications.h>
#endif
This is example Register for remote notifications checking API
In Swift
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
In Obj-C
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
// iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// For iOS 10 display notification (sent via APNS)
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert
| UNAuthorizationOptionSound
| UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
#endif
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
I don't think you can import a framework at runtime in Objective-C.
You can tap into objective c runtime magic and use performSelector: to call selectors/methods without importing a class/framework but you don't necessarily want to use it as there is no compile-time checking on selector names and may result in app crashes if it is not used propertly.
The only way I can think of achieving what you want is to weak-link UserNotifications framework in your library.
On runtime you can check whether your client supports the framework, and if the framework symbols are available, you can run them safely.
Doing it this way, you get the compile time safety of method name checking, and you can write your code in a normal manner but yeah don't forget to check for the availability of the framework before executing any of its symbols.

Unable to receive pushNotifications from Firebase connected through application server

I created a new project in firebase console for apple APNs pushNotifications. I followed all the instructions in firebase documentation like generating SSl certificate and uploading p12 certificates(development and production) to firebase and also the provisioning profile. And also added the whole required code in Appdelegate. And also activated pushNotifications in capabilities and also in Entitlement file APNs enviroment set as development.
When I'm sending a sample message from firebase notification messageing, my iPhone(6) able to receive the notification(send through both FCM token and also with bundelId able to receive notification). But from my application server with same FCM token I'm unable to receive notifications, but my server side getting reply as notification sent Ok with 200 code from firebase.
I am unable to get the solution from last week and also not getting where is issue from mobile side or from server side.
Thanks in Advance....
I got a solution for my problem.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
kGCMMessageIDKey = #"gcm.message_id";
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
// iOS 7.1 or earlier. Disable the deprecation warnings.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIRemoteNotificationType allNotificationTypes =
(UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge);
[application registerForRemoteNotificationTypes:allNotificationTypes];
#pragma clang diagnostic pop
} else {
// iOS 8 or later
// [START register_for_notifications]
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
// iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// For iOS 10 display notification (sent via APNS)
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert
| UNAuthorizationOptionSound
| UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
// For iOS 10 data message (sent via FCM)
[FIRMessaging messaging].remoteMessageDelegate = self;
#endif
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
// [END register_for_notifications]
}
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
[FIRApp configure];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(tokenRefreshNotification:)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
return YES;
}
in my above code I not written
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(tokenRefreshNotification:)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
this line,actually which is not in firebase direct documentation.
And also check whether may get notifications which will print in console by using this method
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
// Print full message
NSLog(#"Notification :%#", remoteMessage.appData);
}
But notification may not be appear on notifications screen,because the apns formate is
{"aps":{"alert":"Testing.. (0)","badge":1,"sound":"default"}}
but from server side it may be a different format.

Resources