Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement push notification for iOS 10[Objective C]?

Can anyone help me with implementing push notification for iOS 10 as i have implemented following code but still getting problem in it:

#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

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];
        }
    }];
}
else {
    // Code for old versions
}

I am getting error suggesting that

Unknown receiver UIUserNotificationCenter

Thank you in advance!

like image 288
Bhargav Soni Avatar asked Oct 04 '16 11:10

Bhargav Soni


People also ask

How do I set up push notifications on iOS?

Setting up Push Notifications in iOS. Go back to the iOS project you created earlier and select the main target. Under the Signing & Capabilities tab, click on the + Capability button, then select Push Notifications. This will enable your app to receive push notifications from OneSignal.

How does push notifications work on the iOS platform?

An iOS push notification is a message that pops up on an Apple device such as an iPhone. Before receiving push notifications from an app, iOS device users must explicitly give permission. Once a user opts-in, mobile app publishers can send push notifications to the users' mobile devices.

How are mobile push notifications implemented?

Android automatically opts in all users to receive push notifications when they install an app. iOS users have to decide at the start whether to permit or deny notifications. Mobile apps need to be registered with the FCM server. This ensures the synchronization of actions.


2 Answers

Sorry guys, I got the answer. I just needed to import UserNotifications framework.

#import <UserNotifications/UserNotifications.h>
like image 195
Bhargav Soni Avatar answered Oct 05 '22 08:10

Bhargav Soni


Check this

#import <UserNotifications/UserNotifications.h>

Then

    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
        UIUserNotificationType allNotificationTypes =
        (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings =
        [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
        [application 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
    }
like image 41
Basir Alam Avatar answered Oct 05 '22 08:10

Basir Alam