In an app which involves push notifications, I am receiving device tokens just fine but the my client's device doesn't receive device token (hence causing failure to receive push notifications). I am using Parse for push notifications. This is what I have in my AppDelegate:
-(void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:newDeviceToken];
PFACL *objectACL = [[PFACL alloc] init];
[objectACL setPublicReadAccess:YES];
[objectACL setPublicWriteAccess:YES];
[currentInstallation saveInBackground];
}
-(void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSString *errorString = [NSString stringWithFormat:#"%#", error];
UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:#"Error" message:errorString delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[errorAlertView show];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[Parse setApplicationId:PARSE_APPLICATION_ID
clientKey:PARSE_CUSTOMER_KEY];
[PFFacebookUtils initializeFacebook];
[self.window makeKeyAndVisible];
// create the Login controller instance:
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
}
Client doesn't receive any UIAlert representing failure of registration with APN so that means didFailToRegisterForRemoteNotificationsWithError is not being called. I have tried removing all installations from my parse server, removing the app & reinstalling again as per guidelines of Apple technical note TN2265. The application doesn't prompt the client for authorization of push notifications on first launch & doesn't appear in notification center either (Which is baffling because didRegisterUserNotificationSettings: is there too). But all this is working fine on my iPad, iPhone 5S, iPod 5, iPhone 4s. Any ideas what Might be causing this device to device variable behavior in the app?!This is the sc client sent me.notificationCenterScreenShot
Try following code
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)];
}
within - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method in your AppDelegate class
Related
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.
This question already has answers here:
didRegisterForRemoteNotificationsWithDeviceToken not called in ios8, but didRegister...Settings is
(19 answers)
Closed 6 years ago.
All are working fine upto today morning, I need device token to move forward
When I debug on the iOS device niether didRegisterForRemoteNotificationsWithDeviceToken nor didFailToRegisterForRemoteNotificationsWithError method is never fired off. I will really appreciate some help. My code follows.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:mySettings];
//[application registerForRemoteNotifications];
}else{
[application registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(#"didRegisterForRemoteNotificationsWithDeviceToken: %#", deviceToken);
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
NSString *deviceID = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
deviceID = [deviceID stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"Device_Token -----> %#\n",deviceID);
[[NSUserDefaults standardUserDefaults] setObject:deviceID forKey:#"KiviDeviceToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(#"Error: %#", error.description);
if (error.code == 3010) {
NSLog(#"Push notifications are not supported in the iOS Simulator.");
} else {
NSLog(#"application:didFailToRegisterForRemoteNotificationsWithError: %#", error);
}
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
NSLog(#"NotificationSettings: %#", notificationSettings);
//register to receive notifications
[application registerForRemoteNotifications];
}
I have checked
iOS settings app notifications are enabled, that method will not get called
I need some help for "Troubleshooting Push Notifications" "If neither delegate callback application:didRegisterForRemoteNotificationsWithDeviceToken: nor application:didFailToRegisterForRemoteNotificationsWithError: is called,
NotificationSettings are getting
I think call back is not calling because connection is not yet been establihed."
There has been issues on Apples end with APNS. For some reason apps in production work fine, but apps which people are developing won't push any notifications whatsoever.
Click here for more info
after deploying FCM cloud messaging which is the upgrading version of GCM cloud messaging as mentioned https://developers.google.com/cloud-messaging/ios/client so we moved to integrate FCM in our app using pod as mentioned https://firebase.google.com/docs/cloud-messaging/notifications i follow up every step in the tutorial here is what happening the remote notification had been received well in both state Background State and active state but the main problem that i faced is the notification does appear in the notification bar here is my code that i am using
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
float ver = [[[UIDevice currentDevice] systemVersion] floatValue];
if(ver >= 8 && ver<9)
{
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerForRemoteNotifications];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
}else if (ver >=9){
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else{
//iOS6 and iOS7 specific code
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert];
}
// ma tensa google maps
[FIRApp configure];
[self connectToFcm];
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSLog(#"My token is: %#", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
NSLog(#"Failed to get token, error: %#", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.applicationIconBadgeNumber = 0;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(#"Message ID: %#", userInfo[#"gcm.message_id"]);
//
// if (application.applicationState == active) {
// <#statements#>
// }
// Pring full message.
NSLog(#"%#", userInfo);
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// [[FIRMessaging messaging] disconnect];
}
- (void)connectToFcm {
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(#"Unable to connect to FCM. %#", error);
} else {
NSLog(#"Connected to FCM.");
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"registeredToFCM"];
[[NSUserDefaults standardUserDefaults] synchronize];
// i used the method since not all the time app register for fcm so when
//the complete launching and did not register //
//for fcm the app request to register fro fcm again//
}
}];
}
here some link that i searched for to solve my problem before moving here Notifications are not getting in iOS 9.2 , FCM background notifications not working in iOS, and Push notifications are not working in iOS 9 but i could not solve my issue
Forgot to tell you that the phone is ringing and vibrating when the notification had been received.
hope any one help Happy Coding !!
If you want the notification to show up you have to set "priority":"high". Like so:
"to":"TOKEN ID",
"notification" : {
"body" : "test"
},
"priority": "high"
}
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.
I am creating an application which support push notification
I am following all the steps.
It give error on simulator
Failed to get token, error: Error Domain=NSCocoaErrorDomain Code=3010 "remote notifications are not supported in the simulator" UserInfo=0x5813d20 {NSLocalizedDescription=remote notifications are not supported in the simulator}
But on device it not calling the delegates methods
didFailToRegisterForRemoteNotificationsWithError
didRegisterForRemoteNotificationsWithDeviceToken
My Code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
NSLog(#"application didFinishLaunchingWithOptions");
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(#"My Token is %#",deviceToken);
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(#"Failed to get token, error: %#", error);
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(#"Received Notification %#", userInfo);
}
Push notification won't work in iPhone's Simulator.
Try to check with device.
Solved for me:
In Device, go to:
`Settings->Notifications->YourApp->Enable_Notifications`
As said by #kalyan Push Notification will NOT work in Simulator
But if you want to get ride of the warning here is the code to work
with both Simulator and device (Check for TARGET_IPHONE_SIMULATOR )
and iOS7 and iOS8 ( Check for registerUserNotificationSettings )
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#if !TARGET_IPHONE_SIMULATOR
// New User Notification Settings for iOS8 support
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else { // for iOS 7
UIRemoteNotificationType remoteNotificationType = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
remoteNotificationType];
}
#endif
}