I follow these steps for push notifications but un able to receive push on my device.
Steps
making and adding push app id and provisioning profile in my project
enable push notifications in my project
making p12 file nd .pem file
sending .pem file to php developer
use following code in my app delegate.m
The code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//ios 9
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
return YES;
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
if (notificationSettings.types != UIUserNotificationTypeNone) {
NSLog(#"didRegisterUser");
[application registerForRemoteNotifications];
}
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
{
NSString *token = [[devToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
token = [token stringByReplacingOccurrencesOfString:#" " withString:#""];
Globals *globlClass = [Globals sharedInstance];
globlClass.DeviceToken = [NSString stringWithFormat:#"%#",token];
NSLog(#"\n\n\n\n\n device token===%#\n\n\n\n",globlClass.DeviceToken);
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(#"failed to regiser %#", err);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"notification options %#", userInfo);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Super" message:#"welcome" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
for (id key in userInfo) {
NSLog(#"key: %#, value: %#", key, [userInfo objectForKey:key]);
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Your title here!" message:#"this gets covered" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
myTextField.text = [userInfo objectForKey:key];
[myAlertView addSubview:myTextField];
[myAlertView show];
}
}
You need to register for remote notifications by call this method [[UIApplication sharedApplication] registerForRemoteNotifications] after registering user notification settings.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//ios 9
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
//call this method here
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
Add below code in your didFinishLaunchingWithOptions in AppDelegate.m:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
working for me for iOS 9.0 and Xcode 7.2.
Related
I'm trying to get iOS device token use code:
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) {
if (!error) {
NSLog(#"request authorization succeeded!");
}
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];
#else
UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#endif
} else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
UIRemoteNotificationType apn_type = (UIRemoteNotificationType) (UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeBadge);
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
}
but i get device token "bGb1GbbR17mB/XCWFpH+YpfyprlSvdy2ZN7aqF8QxHE=" in the get token success callback function.
i use same code in another project can get right device token.
why? please help me!
Try this Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if(CheckOSVersion >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
UIUserNotificationSettings* currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
UIUserNotificationType enabledTypes = currentSettings.types;
BOOL turnedOffFromWithinNotificaitonCenter = ((enabledTypes & UIUserNotificationTypeAlert) == UIUserNotificationTypeAlert);
if (turnedOffFromWithinNotificaitonCenter){
}
else{
}
}
else
{
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types & UIRemoteNotificationTypeAlert)
{
}
else
{
}
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(#"Push Error- %#",[NSString stringWithFormat: #"Error: %#", err]);
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *devToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:#"<"withString:#""]
stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString: #" " withString: #""];
[AppDelegate instance].strToken = devToken;
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"Push"];
[[NSUserDefaults standardUserDefaults] setValue:devToken forKey:#"deviceToken"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSLog(#"Device Token of Device %#",devToken);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"Userinfo%#",userInfo);
}
Note : Require to add push Notification Certificate in your Project
If you have add push Notification certificate than
Try This :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
self.AppDeviceToken=[[NSString alloc] initWithFormat:#"%#",deviceToken];
//NSLog(#"My token is: %#", self.AppDeviceToken);
self.AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:#" " withString:#""];
self.AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:#"<" withString:#""];
self.AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:#">" withString:#""];
NSLog(#"%#'s Device Token is : %#",[[UIDevice currentDevice] name],self.AppDeviceToken);
}
In iOS 10.0 or Greater
#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];
}
}];
}
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
NSLog(#"User Info : %#",notification.request.content.userInfo);
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
NSLog(#"User Info : %#",response.notification.request.content.userInfo);
completionHandler();
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
self.AppDeviceToken=[[NSString alloc] initWithFormat:#"%#",deviceToken];
//NSLog(#"My token is: %#", self.AppDeviceToken);
self.AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:#" " withString:#""];
self.AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:#"<" withString:#""];
self.AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:#">" withString:#""];
NSLog(#"%#'s Device Token is : %#",[[UIDevice currentDevice] name],self.AppDeviceToken);
}
I want to practice APNS , so I need to get device token.
However,I use my iphone 5 can't get device token.
When I use another iphone that is updated ios10 get device token successfully.
What should I do?
This is my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//APNS setting
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]){
UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:mySettings];
[application registerForRemoteNotifications];
}
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSString *key = #"once";
BOOL onceShowed = [[NSUserDefaults standardUserDefaults] boolForKey:key];
if ( !onceShowed ){
// upload device token to my sever
}
}
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
[application registerForRemoteNotifications];
}
Thanks!
Step 1: Register for Push Notification in https://developer.apple.com/notifications/
Step 2: Try this Code.
(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
- (void)application:(UIApplication *)application
didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(#"deviceToken: %#", deviceToken);
NSString * token = [NSString stringWithFormat:#"%#", deviceToken];
//Format token as you need:
token = [token stringByReplacingOccurrencesOfString:#" " withString:#""];
token = [token stringByReplacingOccurrencesOfString:#">" withString:#""];
token = [token stringByReplacingOccurrencesOfString:#"<" withString:#""];
}
I'm not an iOS programmer so bear with me. I'm able to get the push notifications but I get two alerts on my screens.
Issue 1:
-One with the UIAlertView* alertWindow I am creating below
- Second seems to be the default notification alert window. What am I doing wrong thats creating the second window ?
Issue 2: When the device is locked, I can see the notifications on screen but this is the default notification window. I don't see the notification with the details I'm getting in didReceiveRemoteNotification.
Also, I would appreciate If someone can explain the if else block in didFinishLaunchingWithOptions. I copied pasted it.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *AMAZON_SERVER = #"xxxxxx";
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
[Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
configuration.applicationId = APPLICATION_ID;
configuration.clientKey = CLIENT_KEY;
configuration.server = AMAZON_SERVER;
}]];
[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
[Fabric with:#[[Digits class]]];
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
if([[NSUserDefaults standardUserDefaults] boolForKey:#"FirstLaunch"]!=TRUE)
{
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:#"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
//storedevice Type in standardUserDefaults
[self setDeviceType];
return YES;
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(#"Error in registration. Error: %#", err);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
if ([userInfo objectForKey:#"aps"]) {
NSMutableDictionary * apsData = [userInfo objectForKey:#"aps"];
NSString* alert = [apsData objectForKey:#"alert"];
...
...
UIAlertView* alertWindow = [[UIAlertView alloc] initWithTitle: alertHeader
message: message
delegate: self
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[alertWindow show];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
} else {
[PFPush handlePush:userInfo];
}
}
First uninstall the app and try it. It will work fine same problem i am facing.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
configuration.applicationId = APPLICATION_ID;
configuration.clientKey = CLIENT_KEY;
configuration.server = AMAZON_SERVER;
}]];
[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
[Fabric with:#[[Digits class]]];
if([[NSUserDefaults standardUserDefaults] boolForKey:#"FirstLaunch"]!=TRUE)
{
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:#"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
//storedevice Type in standardUserDefaults
[self setDeviceType];
#here i am edited
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
return YES;
}
// remove the below methods
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(#"Error in registration. Error: %#", err);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//[PFPush handlePush:userInfo];
if ([userInfo objectForKey:#"aps"]) {
NSMutableDictionary * apsData = [userInfo objectForKey:#"aps"];
NSString* alert = [apsData objectForKey:#"alert"];
...
...
UIAlertView* alertWindow = [[UIAlertView alloc] initWithTitle: alertHeader
message: message
delegate: self
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[alertWindow show];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
} else {
[PFPush handlePush:userInfo];
}
}
Your if statement says that if your [UIApplication sharedApplication] instance has implemented registerUserNotificationSettings: method, then it will execute if block, otherwise it will execute else block.
I am working on iPhone application using Push Notification. I am having a notification issue, I am getting device token id successfully in iOS 6, 7, 8.1, 8.1.2, but for my iPhone 6 with iOS 8.0.
So in my push notification method "didRegisterForRemoteNotificationsWithDeviceToken", token id is returning nil. So, I can't able to get push notification. I don't know what is the problem.
Here is my code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
else{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
.
.
.
.
[self.window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString *newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:#" " withString:#""];
[GlobalVariables proTrain].deviceToken = newToken;
NSLog(#"device token id = %#", newToken);
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
if (IS_OS_8_OR_LATER)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}
Maybe instead of this:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
Try this:
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
/*--- Setup Push Notification ---*/
//For iOS 8
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)] && [UIApplication instancesRespondToSelector:#selector(registerForRemoteNotifications)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
//For iOS 7 & less
else if ([UIApplication instancesRespondToSelector:#selector(registerForRemoteNotificationTypes:)])
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
The notifications methods are as follows :
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
//handle the actions
if ([identifier isEqualToString:#"declineAction"]){
}
else if ([identifier isEqualToString:#"answerAction"]){
NSLog(#"didReceiveRemoteNotification ios 8");// push some specific view when notification received
}
}
#endif
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
self.strDeviceToken = [[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]] stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"Device Token ==== %#", self.strDeviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(#"error : %#",error.localizedDescription);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"didReceiveRemoteNotification ios 7");// push some specific view when notification received
}
if ([application respondsToSelector:#selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
else
{
// iOS < 8 Notifications
[application registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
In the notifications app is not registred for son and badge.
This is my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
//-- Set Notification
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.000000) {
UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
//[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
Someone has an explanation? Thank you
iOS 8 need some change in push notification delegate methods and explained clearly in this link
Also don't call registerForRemoteNotifications for iOS 8 because its deprecated.
Hi please use this code,
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
} else {
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
}
#else
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
#endif
and for delegate methods,
#pragma mark - Push Notification
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Prepare the Device Token for Registration (remove spaces and < >)
NSString *TokenID = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:#"<"withString:#""]
stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString: #" " withString: #""];
[ApplicationPreferences setTokenId:TokenID];
if (DEBUG_MODE) {
NSLog(#"device token - %#",[NSString stringWithFormat:#"Device Token = %#",TokenID]);
NSUUID *oNSUUID = [[UIDevice currentDevice] identifierForVendor];
NSLog(#"Vendor token - %#",[NSString stringWithFormat:#"Device Token = %#",[oNSUUID UUIDString]]);
}
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
if (DEBUG_MODE) {
NSLog(#"Push Error- %#",[NSString stringWithFormat: #"Error: %#", err]);
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
}
In the iOS 8 version you forgot the brackets
(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound)