this is my code for apple push notification, when the application is running and notification coming i am incrementing the badge count and getting desired result when i click home button, on app icon. but when i am not running my application and notification comes, it didn't auto increment badge count and remains to only 1. the value 1 is coming from server. can any one point out where i am doing wrong. thanks in advance.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
userMessageCounter = #"0";
postType = 0;
joinedStreamChecker = 0;
OwnerValue = 0;
pushValue = 1;
badgeValue =0;
// Override point for customization after application launch.
// Add the navigation controller's view to the window and display.
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
//[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
//(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
// [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeNewsstandContentAvailability | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
{
pushValue = 0;
NSLog(#"notification off");
}
return YES;
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken1 {
NSString *str = [NSString
stringWithFormat:#"%#",deviceToken1];
NSLog(#"%#",str);
self.deviceToken = [NSString stringWithFormat:#"%#",str];
NSLog(#"dev --- %#",self.deviceToken);
self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:#"<" withString:#""];
self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:#" " withString:#""];
self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:#">" withString:#""];
NSLog(#"dev --- %#",self.deviceToken);
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSString *str = [NSString stringWithFormat: #"Error: %#", err];
NSLog(#"%#",str);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"Received notification: %#", userInfo);
//[self addMessageFromRemoteNotification:userInfo];
NSString* alertValue = [[userInfo valueForKey:#"aps"] valueForKey:#"badge"];
NSLog(#"my message-- %#",alertValue);
badgeValue= [alertValue intValue];
[UIApplication sharedApplication].applicationIconBadgeNumber += badgeValue;
//badgeValue = [UIApplication sharedApplication].applicationIconBadgeNumber;
//[UIApplication sharedApplication].applicationIconBadgeNumber=0;
//[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeValue];
}
Apple does not keep track of your data for you. It only shows what you tell it to. Thus, you have to store the count on your server, then tell apple the new badge number when you send the alert. Typically this is done by having the app phone home when launched telling your servers to zero out the count of unread notifications.
Related
here my code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes: (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
[application registerForRemoteNotifications];
return YES;
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
NSLog(#"device registered...");
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
// [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
NSString *deviceID = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
deviceID = [deviceID stringByReplacingOccurrencesOfString:#" " withString:#""];
str_DeviceToken = deviceID;
NSLog(#"device token is %#",str_DeviceToken);
}
I neither get any device token error nor get device token when i run this code into device.
I spent a day behind find the issue please help to get device token.
Thanks in advance
I am not getting push in production Mode . while it work fine in developer mode.Here is my code I am newer in iOS and i am first time implementation APN. Please help. I am stuck in lot of time.
if (iOSversion >=8) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication]registerForRemoteNotifications];
}
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)tokenss
{
NSUInteger rntypes;
NSString *device=[[UIDevice currentDevice]systemVersion];
int ar=[device intValue];
if (ar>=8) {
rntypes = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
}
NSString *deviceToken =[self stringWithDeviceToken:tokenss];
NSLog(#"Device token: %#", deviceToken);
callBackToken =deviceToken;
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
NSString *errorString = [NSString stringWithFormat: #"Error: %#", err];
NSLog(#"%#",errorString);
}
- (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"]){
}
}
Such problems are due to current Provisioning Profile does not contain APNS information, as the Provisioning Profile is created BEFORE creating the APNS certificate.
Therefore, to solve the problem, create a new Provisioning Profile & select the Provisioning Profile in Xcode will clear the error.
Also, when you add push, I believe you have updated the certificates in keychain too.
I have an app in iOS where I am sending quick surveys to the users. I want to make use of the new funcionality in iOS 8 where the user interacts directly on the push notification. The user will receive the survey question and respond.
I know you have to construct your actions and categories when starting the app. I have written my code and have been able to successfully perform this so far.
Now, the thing is that the text on the action button will change based on the survey question. I am including the keys and text for the action buttons on the JSON I am sending, but I haven't been able to change the action button titles when receiving the notification.
Is there a way to do this? Here is my code:
I define global variables:
//This will help configure the interactive notifications, so that the user can reply to push notification from out of the app
NSString * const notificationCategoryIdent = #"SURVEY";
NSString * const notificationActionPositiveButton = #"POSITIVE_BUTTON";
NSString * const notificationActionNegativeButton = #"NEGATIVE_BUTTON";
In my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if(pushNotificationPayload) {
urlPushData2 = #"Application was closed";
[self application:[UIApplication sharedApplication] didReceiveRemoteNotification:pushNotificationPayload];
}
//Registrar tipos de aviso para Push Notifications
//if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]){
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0){ //Before iOS 8
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)];
}
else {
// define notification actions (In case of categorized remote push notifications)
positiveButton = [[UIMutableUserNotificationAction alloc] init];
[positiveButton setActivationMode:UIUserNotificationActivationModeForeground];
[positiveButton setTitle:notificationActionPositiveButton];
[positiveButton setIdentifier:notificationActionPositiveButton];
[positiveButton setDestructive:NO];
// this action wil be executed without necessity of passcode authentication (if locked)
[positiveButton setAuthenticationRequired:NO];
negativeButton = [[UIMutableUserNotificationAction alloc] init];
[negativeButton setActivationMode:UIUserNotificationActivationModeForeground];
[negativeButton setTitle:notificationActionNegativeButton];
[negativeButton setIdentifier:notificationActionNegativeButton];
[negativeButton setDestructive:NO];
// this action wil be executed without necessity of passcode authentication (if locked)
[negativeButton setAuthenticationRequired:NO];
// define Categories (In case of categorized remote push notifications)
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:notificationCategoryIdent];
[actionCategory setActions:#[positiveButton, negativeButton]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
//Boolean value for temporary app interruptions
temporaryInactivity = FALSE;
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"remote notification: %#",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:#"aps"];
NSString *alert = [apsInfo objectForKey:#"alert"];
//NSLog(#"Received Push Alert: %#", alert);
NSString *sound = [apsInfo objectForKey:#"sound"];
NSLog(#"Received Push Sound: %#", sound);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:#"badge"] integerValue];
self.mainUrl = [userInfo valueForKey:#"url"];
urlPushData = [NSString stringWithFormat:#"%#", mainUrl];
//NSLog(#"urlPushData: %#",urlPushData);
self.mainUrl2 = [userInfo valueForKey:#"url2"];
urlPushData2 = [NSString stringWithFormat:#"%#", mainUrl2];
//NSLog(#"urlPushData2: %#",urlPushData2);
NSString *category = [apsInfo objectForKey:#"category"];
NSLog(#"category: %#", category);
if ([category isEqualToString:notificationCategoryIdent]) {
NSLog(#"button1: %#", [apsInfo objectForKey:#"positiveButton"]);
NSLog(#"button2: %#", [apsInfo objectForKey:#"negativeButton"]);
NSString *stringPositiveButton = [apsInfo objectForKey:#"positiveButton"];
NSString *stringNegativeButton = [apsInfo objectForKey:#"negativeButton"];
[positiveButton setTitle:stringPositiveButton];
[negativeButton setTitle:stringNegativeButton];
}
else {
if (![urlPushData2 isEqualToString:#"Application was closed"]) {
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber - 1;
[[NSUserDefaults standardUserDefaults] setObject:urlPushData2 forKey:#"pushNotificationURL"];
[[NSUserDefaults standardUserDefaults] setObject:alert forKey:#"pushNotificationURLName"];
[[NSUserDefaults standardUserDefaults]synchronize];
if (NSClassFromString(#"UIAlertController")) {
//make and use a UIAlertController
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:#"%#",appNameLink]
message:[NSString stringWithFormat:#"Has recibido una nueva nota: %#. ¿Deseas verla?",alert]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *Cancel = [UIAlertAction actionWithTitle:#"No"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber - 1;
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"pushNotificationURL"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"pushNotificationURLName"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
];
UIAlertAction *OK = [UIAlertAction actionWithTitle:#"Sí"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber - 1;
self.url = [[NSUserDefaults standardUserDefaults] objectForKey:#"pushNotificationURL"];
self.urlName = [[NSUserDefaults standardUserDefaults] objectForKey:#"pushNotificationURLName"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"pushNotificationURL"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"pushNotificationURLName"];
[[NSUserDefaults standardUserDefaults]synchronize];
[self setupNavigationControllerApp];
}
];
[alertView addAction:Cancel];
[alertView addAction:OK];
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:alertView animated:YES completion:nil];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:#"%#",appNameLink] message:[NSString stringWithFormat:#"Has recibido una nueva nota: %#. ¿Deseas verla?",alert] delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Sí", nil];
[alertView show];
}
} else {
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber - 1;
//self.url = [[NSUserDefaults standardUserDefaults]objectForKey:#"pushNotificationURL"];
self.url = urlPushData2;
self.urlName = alert;
[self setupNavigationControllerApp];
}
}
}
- (void) application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:notificationActionPositiveButton]) {
//Testing the action button
self.url = #"http://www.google.com";
self.urlName = #"hello";
[self setupNavigationControllerApp];
}
else if ([identifier isEqualToString:notificationActionNegativeButton]) {
//Testing the action button
self.url = #"http://www.yahoo.com";
self.urlName = #"goodbye";
[self setupNavigationControllerApp];
}
// Must be called when finished
completionHandler();
}
And this is my JSON:
{"aps":
{"alert":"Who's your favorite pop singer?",
"badge":1,
"sound":"default",
"category":"SURVEY",
"positiveButton":"Katy Perry",
"negativeButton":"Miley Cyrus"
},
"url":"http://www.pretechmobile.com",
"url2":"http://goo.gl/U9p1y"
}
I am sorry, but you cannot change the title of the buttons dynamically. They are tied to the category in which they are stored when you register for notifications.
A possible workaround could be to present the two options in the text of the message labeled with an 'A' and a 'B' and have your buttons with titles 'Option A' and 'Option B'. Something like this:
---------------------------------
|Who's your favorite pop singer?|
|A: Katy Perry B: Miley Cyrus|
---------------------------------
| Option A | Option B |
---------------------------------
Also, If you have yes/no questions, you can create a category for them with the options 'Yes' and 'No' and label your notifications with this category.
I hope this helps a bit.
I am getting push notification from server but my postNotification is not being called. It is correctly entering in if statement which states that type == 1. All the user defaults are correctly stored but the notification is not being called. I put the break point as well as an alert in the function of view controller which is called by the post notification. But it never being called. Here is my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if(launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSString *type = [dictionary objectForKey:#"type"];
if([[NSString stringWithFormat:#"%#",type]integerValue]==1)
{
[[NSUserDefaults standardUserDefaults]setObject:[[dictionary objectForKey:#"aps"] valueForKey:#"alert"] forKey:#"message"];
[[NSUserDefaults standardUserDefaults]setObject:[dictionary objectForKey:#"senderid"] forKey:#"senderID"];
[[NSUserDefaults standardUserDefaults]setObject:[dictionary objectForKey:#"sendername"] forKey:#"sendername"];
[[NSUserDefaults standardUserDefaults]setObject:[dictionary objectForKey:#"userimage"] forKey:#"pushImage"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSLog(#"message is %#",[[dictionary objectForKey:#"aps"] valueForKey:#"alert"]);
NSLog(#"senderID is %#",[dictionary objectForKey:#"senderid"]);
NSLog(#"sender name is %#",[dictionary objectForKey:#"sendername"]);
[[NSNotificationCenter defaultCenter] postNotificationName:#"pushNotification" object:nil userInfo:launchOptions];
}
}
#ifdef __IPHONE_8_0
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
#endif
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeNewsstandContentAvailability;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
application.applicationIconBadgeNumber=0;
[UIApplication sharedApplication].statusBarHidden = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible=FALSE;
return YES;
}
Find the answer of my own question. Here is the code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[self window] setBackgroundColor:[UIColor whiteColor]];
[FBLoginView class];
self.mainNavigationController = (UINavigationController*)self.window.rootViewController;
if(launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSString *type = [NSString stringWithFormat:#"%#",[[[dictionary valueForKey:#"aps"] valueForKey:#"json"] valueForKey:#"type"]];
NSLog(#"type is %#",type);
if ([type isEqualToString:#"0"]) // 0 means NOW
{
[[NSUserDefaults standardUserDefaults]setObject:[[[dictionary valueForKey:#"aps"] valueForKey:#"json"] valueForKey:#"yadda_sub_type"] forKey:kYaddaSubType_Push];
[[NSUserDefaults standardUserDefaults]setObject:[[[dictionary valueForKey:#"aps"] valueForKey:#"json"] valueForKey:#"user_id"] forKey:kYaddaUserID_Push];
[[NSUserDefaults standardUserDefaults]setObject:[[[dictionary valueForKey:#"aps"] valueForKey:#"json"] valueForKey:#"yadda_id"] forKey:kYaddaID_Push];
ReciversSideVC *reciverControoler = [[ReciversSideVC alloc]init];
[reciverControoler.navigationController setNavigationBarHidden:YES];
[reciverControoler.navigationController setToolbarHidden:YES];
[AppDelegateClass.mainNavigationController pushViewController:reciverControoler animated:NO];
}
else if ([type isEqualToString:#"1"]) // 1 means LATER
{
if (isCalendarScreenOpen==YES)
{
CalendarVC *calendar = [[CalendarVC alloc]init];
[calendar.navigationController setNavigationBarHidden:YES];
[calendar.navigationController setToolbarHidden:YES];
[AppDelegateClass.mainNavigationController pushViewController:calendar animated:NO];
}
}
else if ([type isEqualToString:#"2"]) // 2 means Remind me
{
LandingPageVC *landing = [[LandingPageVC alloc]init];
[landing.navigationController setNavigationBarHidden:YES];
[landing.navigationController setToolbarHidden:YES];
[AppDelegateClass.mainNavigationController pushViewController:landing animated:NO];
}
else if ([type isEqualToString:#"3"]) // 3 means Reply
{
LandingPageVC *landing = [[LandingPageVC alloc]init];
[landing.navigationController setNavigationBarHidden:YES];
[landing.navigationController setToolbarHidden:YES];
[AppDelegateClass.mainNavigationController pushViewController:landing animated:NO];
}
else if ([type isEqualToString:#"4"]) // 4 means Contacts
{
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:#"flag_CallAPI"];
[[NSUserDefaults standardUserDefaults]synchronize];
ContactDetailVC *contactDetails = [[ContactDetailVC alloc]init];
contactDetails.userID = [[[dictionary valueForKey:#"aps"] valueForKey:#"json"] valueForKey:#"user_id"];
contactDetails.phoneNumberFromServer = [[[dictionary valueForKey:#"aps"] valueForKey:#"json"] valueForKey:#"phone_number"];
[contactDetails.navigationController setNavigationBarHidden:YES];
[contactDetails.navigationController setToolbarHidden:YES];
[AppDelegateClass.mainNavigationController pushViewController:contactDetails animated:NO];
}
}
}
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif != nil)
{
NSDictionary *NotificationValue = [localNotif.userInfo objectForKey:#"localPushNotification"];
if (NotificationValue != nil)
{
NSLog(#"Run with notification value: %#", NotificationValue);
[[NSUserDefaults standardUserDefaults]setObject:[NotificationValue valueForKey:#"yadda_sub_type"] forKey:kYaddaSubType_Push];
[[NSUserDefaults standardUserDefaults]setObject:[NotificationValue valueForKey:#"user_id"] forKey:kYaddaUserID_Push];
[[NSUserDefaults standardUserDefaults]setObject:[NotificationValue valueForKey:#"yadda_id"] forKey:kYaddaID_Push];
ReciversSideVC *reciverControoler = [[ReciversSideVC alloc]init];
[AppDelegateClass.mainNavigationController pushViewController:reciverControoler animated:NO];
}
}
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeNewsstandContentAvailability;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
application.applicationIconBadgeNumber=0;
[UIApplication sharedApplication].statusBarHidden = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible=FALSE;
return YES;
}
I am new to Objective C. In my app i need to implement push notification, i have created new app ID and i have created new provisioning profile also. i have finished all the steps mentioned in this link
i have declared this delegate function in my appDelegate .m file.
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *deviceTokens = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
deviceTokens = [deviceTokens stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"registered device token %#", deviceTokens);
self.deviceToken = deviceTokens;
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSString *str = [NSString stringWithFormat: #"Error: %#", err];
NSLog(#"String %#",str);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
for (id key in userInfo) {
NSLog(#"key: %#, value: %#", key, [userInfo objectForKey:key]);
}
}
But this delegate functions are not called. please help me to fix this issue.
In your AppDelegated didfinishLaunching with options write the following line
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
Add this code to your application didFinishLaunchingWithOptions method, inside your app delegate
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
//We check because ios7 cannot respond to this method and there will be a crash..
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
[application registerForRemoteNotifications];
}
else {
//This will work on ios7 devices and below
UIRemoteNotificationType types = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:types];
[application registerForRemoteNotifications];
}
return YES;
Use new delegate method
Swift:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)