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
}
Related
I am trying to implement push notifications in my application. I am using APNs Authentication Key. Every guide I found so far has either deprecated method or it's in Swift. I am interested in a guide like this one but in Objective C.
What I have done so far, inside AppDelegate.m :
#import Firebase;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[FIRApp configure];
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self application:application didReceiveRemoteNotification:userInfo];
}
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Perform different operations depending on the scenario
if (application.applicationState == UIApplicationStateInactive) {
// Opening the app
NSLog(#"Firebase opening app");
}
else if (application.applicationState == UIApplicationStateBackground) {
// Coming from background
NSLog(#"Firebase background");
}
else {
// Active
NSLog(#"Firebase active");
}
}
Unfortunately when I send a message from firebase I get nothing, no output in xcode Console and no notification.
I would appreciate it if you can point me to the right direction.
You do not implement the firebase apn token neither you connect to the database. Why not just implement it like here firebase example.
Did you even setup the plist and the POD libs? In the code example above there is only deprecated code for the iOS part of version 8/9 - means u can ignore these code lines.
Please use this method:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//Do something
}
instead of this:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//Do something
}
Update:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[FIRApp configure];
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)])
{
#ifdef __IPHONE_8_0
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert
| UIUserNotificationTypeBadge
| UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
#endif
}
else
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
return YES;
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
}
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
My iOS app stopped receiving push notifications although I upgraded the code as per the documentations and this.
Here's the code I'm using:
In my AppDelegate's didFinishLaunchingWithOptions:
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:UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound];
}
The didRegisterForRemoteNotificationsWithDeviceToken method is getting called, as it was before, so everything seems fine.
Also, my test device has the notifications enabled.
But when sending a push from Parse.com it no longer arrives.
EDIT 1:
None of the answers work. I updated my Parse.com framework to version 1.6.2 (the latest) which doesn't help either, and I'm copying my code again - this time the updated version based on the answers:
Inside didFinishLaunchingWithOptions:
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
// [application registerForRemoteNotifications];
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
And these are the delegate methods:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
NSLog(#"didRegisterForRemoteNotificationsWithDeviceToken CALLED");
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:newDeviceToken];
[currentInstallation addUniqueObject:#"Test8Channel" forKey:#"channels"];
if([PFUser currentUser]/* && [[PFUser currentUser] objectId] != nil*/) {
[currentInstallation addUniqueObject:[PFUser currentUser] forKey:kOwnerKey];
}
[currentInstallation saveInBackground];
}
#ifdef IS_OS_8_OR_LATER
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
NSLog(#"didRegisterUserNotificationSettings CALLED");
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString*)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler {
NSLog(#"handleActionWithIdentifier CALLED");
//handle the actions
if ([identifier isEqualToString:#"declineAction"]){
NSLog(#"handleActionWithIdentifier %#", #"declineAction");
}
else if ([identifier isEqualToString:#"answerAction"]){
NSLog(#"handleActionWithIdentifier %#", #"answerAction");
}
}
#endif
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
if (error.code == 3010) {
NSLog(#"Push notifications are not supported in the iOS Simulator.");
} else {
// show some alert or otherwise handle the failure to register.
NSLog(#"application:didFailToRegisterForRemoteNotificationsWithError: %#", error);
}
}
Both didRegisterUserNotificationSettings and didRegisterForRemoteNotificationsWithDeviceToken are getting called and it seems fine. But the push doesn't arrive.
EDIT 2:
I'm noticing that if I call both
[application registerUserNotificationSettings:settings];
and
[application registerForRemoteNotifications];
inside the if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
the delegate's didRegisterForRemoteNotificationsWithDeviceToken is getting called twice. I'm not sure how meaningful this is.
Getting desperate here.
//-- Set Notification
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:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
Use this simple conditional block for handle it. because some methods/classes are updated with version
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
//ios8
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
.......
}
}
else
{
// ios7
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerForRemoteNotificationTypes:)])
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
.........
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self enableNotifications:application];
return YES;
}
#define iOS8_OR_NEWER ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 8.0 )
- (void)enableNotifications:(UIApplication *)application
{
if (iOS8_OR_NEWER) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
}
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
// https://nrj.io/simple-interactive-notifications-in-ios-8
}
#endif
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(#"Push Token : %#", deviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(#"Failed to get token, error: %#", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"didReceiveRemoteNotification : %#", userInfo);
}
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
I'm trying to check if PushNotifications for my app are enabled.
In AppDelegate.m I do register app for remote notifications, and in Settings on iPhone (iOS 8) Push Notifications for this app are enabled.
I've googled to methods:
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
BOOL check = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
As the result, types = UIRemoteNotificationTypeNone and check = NO.
I'm using this code sample to register application for Push Notifications:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
//-- Set Notification
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:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
//--- your custom code
return YES;
}
What is the problem here?
I think you've already found a solution but just in case: in iOS 8, enabledRemoteNoficationTypes is deprecated you should use:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(currentUserNotificationSettings)]) {
UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (currentSettings.types == UIUserNotificationTypeNone) {}
Best
Try checking your state with this code:
UIRemoteNotificationType allOnType = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge ;
UIRemoteNotificationType offType = UIRemoteNotificationTypeNone ;
UIRemoteNotificationType currentTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (currentTypes == allOnType) {
// all are on
} else if (currentTypes == offType) {
// all are off
} else {
// some are on, some are off
}
Edit
Also, try implementing the Push Notification Callbacks:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(#"Token%#",deviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
NSLog(#"err:%#",err);
}
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//for iOS8
}