Soumen Sardar Soumen Sardar - 22 days ago 12
Objective-C Question

iOS 10 device , not receiving notification in production mode, but development is working

My app is live in iTunes Connect. To give support of

iOS 10
I am using same
certificate
and
provisioning
that I was using in the last version. Currently, my
XCode
version is
8.1 (8B62)
. I am receiving remote notifications in development. But when I am testing the app in
TestFlight
, I am not receiving any remote notification. But It was working fine up to
iOS 9
with
XCode 7
.

Here is my
AppDelegate.h


#import <UIKit/UIKit.h>


#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@import UserNotifications;
#endif

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
#else
@interface AppDelegate : UIResponder <UIApplicationDelegate>
#endif

@property (strong, nonatomic) UIWindow *window;
@property (copy ,nonatomic) void(^backgroundSessionCompletionHandler)();


@end

// Copied from Apple's header in case it is missing in some cases (e.g. pre-Xcode 8 builds).
#ifndef NSFoundationVersionNumber_iOS_9_x_Max
#define NSFoundationVersionNumber_iOS_9_x_Max 1299
#endif


Here is my
AppDelegate.m


#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

#pragma mark - Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


[self initPushnotificationService];

[self decideViewController];

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
// SOME CODE
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
// SOME CODE
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
// SOME CODE
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
// SOME CODE
}


- (void)applicationWillTerminate:(UIApplication *)application
{
// SOME CODE
}

- (void)application:(UIApplication *)app
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
{
// SOME CODE
}

- (void)application:(UIApplication *)app
didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {

// SOME CODE
}


- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forLocalNotification:(UILocalNotification *)notification
withResponseInfo:(NSDictionary *)responseInfo
completionHandler:(void (^)())completionHandler{

// SOME CODE
}

- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(UILocalNotification *)notification
withResponseInfo:(NSDictionary *)responseInfo
completionHandler:(void (^)())completionHandler{

// SOME CODE
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// SOME CODE
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

// SOME CODE

}

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
#pragma mark -
#pragma mark - UNUserNotificationCenterDelegate
#pragma mark FOREGROUND Delegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

// SOME CODE
}
#pragma mark BACKGROUND Delegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {

// SOME CODE
}
#endif


#pragma mark - push notification setup
-(void)initPushnotificationService{

UIMutableUserNotificationAction *viewMessageAction;
viewMessageAction = [[UIMutableUserNotificationAction alloc] init];
[viewMessageAction setActivationMode:UIUserNotificationActivationModeBackground];
[viewMessageAction setTitle:@"Say OK"];
[viewMessageAction setIdentifier:NotificationActionSayOkMessage];
[viewMessageAction setDestructive:NO];
[viewMessageAction setAuthenticationRequired:NO];

UIMutableUserNotificationAction *replyMessageAction;
if( __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0){
replyMessageAction = [[UIMutableUserNotificationAction alloc] init];
[replyMessageAction setActivationMode:UIUserNotificationActivationModeBackground];
[replyMessageAction setTitle:@"Reply"];
[replyMessageAction setBehavior:UIUserNotificationActionBehaviorTextInput];
[replyMessageAction setIdentifier:NotificationActionReplyMessage];
[replyMessageAction setDestructive:NO];
[replyMessageAction setAuthenticationRequired:NO];
}
else{
replyMessageAction = [[UIMutableUserNotificationAction alloc] init];
[replyMessageAction setActivationMode:UIUserNotificationActivationModeForeground];
[replyMessageAction setTitle:@"Reply"];
[replyMessageAction setIdentifier:NotificationActionReplyMessage];
[replyMessageAction setDestructive:NO];
[replyMessageAction setAuthenticationRequired:NO];
}

UIMutableUserNotificationCategory *viewMessageActionactionCategory;
viewMessageActionactionCategory = [[UIMutableUserNotificationCategory alloc] init];
[viewMessageActionactionCategory setIdentifier:NotificationCategoryViewMessage];
[viewMessageActionactionCategory setActions:@[viewMessageAction, replyMessageAction]
forContext:UIUserNotificationActionContextDefault];

NSSet *categories = [NSSet setWithObject:viewMessageActionactionCategory];

// Register for remote notifications
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"
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
#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:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else{
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0


UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;

[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if( !error ){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}

@try {
UNNotificationAction *sayOkMessageAction = [UNNotificationAction actionWithIdentifier:NotificationActionSayOkMessage title:@"Say OK" options:UNNotificationActionOptionAuthenticationRequired];


UNTextInputNotificationAction *replyMessageAction = [UNTextInputNotificationAction actionWithIdentifier:NotificationActionReplyMessage title:@"Reply" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"Reply" textInputPlaceholder:@"Message"];


UNNotificationCategory *messageCategory = [UNNotificationCategory categoryWithIdentifier:NotificationCategoryViewMessage actions:@[sayOkMessageAction,replyMessageAction] intentIdentifiers:@[NotificationActionSayOkMessage,NotificationActionReplyMessage] options:UNNotificationCategoryOptionCustomDismissAction];

NSSet *categories_set = [NSSet setWithObject:messageCategory];

[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories_set];

/// For iOS 10 display notification (sent via APNS)
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];


}
@catch (NSException *exception) {
NSLog(@"%@",exception);
}
}
];

#endif
}
}

}


@end

Answer

enter image description here@soumenSardar - For new xode 8.1, you have to add a entitlement file

Targets ---> Capabilities and verify Push Notifications is all correct on that view --> Fix it.

It should works

Be careful push notifications are off by default in xcode 8.1