I get a new iPhone and when i tried to test it with a Xcode project with push notifications , it doesn't get token , always failed to get token , i think this because when i was creating the certificate i didn't mark on the device , now i want to add this device to the testing and get token what i should do ??
- (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);
}
For that you should first set Provisioning profile in which you have added your device UDID.
then Add below code to register Pushnotification in device. so when you run your app first time it will ask you for permission .
//Push Noti
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound)];
If you allow then below delegate will be called. and it will give you device token.
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *strDeviceToken = [[[[[deviceToken description]stringByReplacingOccurrencesOfString: #"<" withString: #""] stringByReplacingOccurrencesOfString: #">" withString: #""]stringByReplacingOccurrencesOfString:#" " withString: #""]copy];
NSLog(#"%#",strDeviceToken);
}
If you dont allow then below delegate method will be called
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(#"Failed to get token, error: %#", error);
}
Maybe it will help you.
Related
I am Using Firebase phone number login authentication All things are perfect
1) Provisioning profile
2) certificate
3) Signing methods Enable
4) Project setting with .12 file
5) everything should perfect
Issue
When I send mobile number for the OTP using thins method using this code
NSString *phoneNumber = #"+919428936703";
[[FIRPhoneAuthProvider provider]
verifyPhoneNumber:phoneNumber
completion:^(NSString * verificationID,
NSError * error) {
NSLog(#"VARIFICATION CODE %#", verificationID);
NSLog(#"Error %#", error);
if (!error){
}else{
}
}];
Also get call methods
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Pass device token to auth.
[[FIRAuth auth] setAPNSToken:deviceToken type:FIRAuthAPNSTokenTypeSandbox];
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)notification
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(#"DATA OF AUTH %#", [FIRAuth auth]);
if ([[FIRAuth auth] canHandleNotification:notification]) {
NSLog(#"Handle by Firebase ");
completionHandler(UIBackgroundFetchResultNoData);
return;
}else{
NSLog(#"NOT HANDLE BY FIREBASE %#", notification);
}
}
but then after getting a crash with this error log
-[__NSCFString setFir_authPhoneNumber:]: unrecognized selector sent to instance 0x166388b0
It looks like you're not linking your app with -ObjC linker flag, which is part of the instructions for Integrate without CocoaPods.
setFir_authPhoneNumber: is implemented as a category, thus -ObjC linker flag must be used or the compiled .o from the library won't be linked into your app binary.
I have integrated Firebase cloud messaging in my iOS application without cocoa pods. Firebase analytics is working fine. But FCM token is received on simulator but not real device. On real device I keep getting error
Failed to fetch default token Error Domain=com.firebase.iid Code=501
"(null)"
I have uploaded the .p12 certificates for development and prod on Firebase
I have checked the bundle ID for my app and that on Firebase console
I have Push notification enable on my App ID.
Here is my code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[FIRApp configure];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(tokenRefreshNotification:) name:kFIRInstanceIDTokenRefreshNotification object:nil];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
// For iOS 10 display notification (sent via APNS)
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
// For iOS 10 data message (sent via FCM)
[FIRMessaging messaging].remoteMessageDelegate = self;
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
}
- (void)tokenRefreshNotification:(NSNotification *)notification {
// Note that this callback will be fired everytime a new token is generated, including the first
// time. So if you need to retrieve the token as soon as it is available this is where that
// should be done.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(#"InstanceID token: %#", refreshedToken);
// Connect to FCM since connection may have failed when attempted before having a token.
[self connectToFcm];
// TODO: If necessary send token to application server.
}
- (void)connectToFcm {
// Won't connect since there is no token
if (![[FIRInstanceID instanceID] token]) {
return;
}
// Disconnect previous FCM connection if it exists.
[[FIRMessaging messaging] disconnect];
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(#"Unable to connect to FCM. %#", error);
} else {
NSLog(#"Connected to FCM. FCM token - %#", [[FIRInstanceID instanceID] token] );
}
}];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
[self connectToFcm];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[FIRMessaging messaging] disconnect];
NSLog(#"Disconnected from FCM");
}
Please help.
I got solution to my own problem. The device's date and time was incorrect. The second I changed it to current date & time, Firebase started giving me FCM token and got connected properly. I have checked the push notification as well from Firebase Notification Console. It is working in a way better than I imagined.
May be the issue is due the version problem.Actually me also faced the same problem.I got the FCM token on simulator but not on the device.The reason is due to registering the User notifications setting on didFinishLaunchingWithOptions..... We have to check the version on both device and simulator... If both are not same..please check the " [[UIApplication sharedApplication] registerUserNotificationSettings:settings] "
conditions for the current device version... Bcz I restricted this for >= 10 versions in didFinishLaunchingWithOptions....
In the following code, the device token is gotten.
But Alert View which confirms push notification permission does not appear.
Maybe for that reason, although sending push notification, it does not reach the device.
Do you have any idea why the Alert does not appear?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *deviceTokenString = [deviceToken.description stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"DeviceToken: %#", deviceTokenString);
}
Deployment target is iOS8.0.
Push Notifications status in Provisioning Profile is "Enabled".
I am trying to test my app on iPod touch 4, however I need APNS support.
I found out that neither delegate callbacks for APNS is called.
I've searched through the Technical Note, and it doesn't help (for the same code works for my iPad2, that the device token is returned).
Any idea on this?
I am using iPod touch 4 with iOS 5.1.1
Edit:
Ah, more information provided here.
I have create a provisioning profile before as well as all those certificates. That's why I can work that out with iPad.
Then today, I just added my iPod touch 4 as the development device and downloaded the provisioning profile again. Will this act not registering my iPod touch 4 to be able for handling APNS job????
Edit 2:
As requested:
Actually I just do whatever Apple requested me to do,
that I included
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge)];
//Other codes...
}
Then, the two delegates callback methods
#pragma mark - For Apple Push Notification Service
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *str = [NSString
stringWithFormat:#"%#",deviceToken];
NSString *devToken = [[[[str description]
stringByReplacingOccurrencesOfString:#"<"withString:#""]
stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString: #" " withString: #""];
NSLog(#"%#", devToken);
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults)
{
[standardUserDefaults setObject:devToken forKey:#"device_token"];
[standardUserDefaults synchronize];
}
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(#"application:didFailToRegisterForRemoteNotificationsWithError: %#", error);
}
Actually, I just copy and paste what Apple provided me.
However the case is just as mentioned above, either method is called for iPad2 with iOS 5.0.1, iPhone with iOS 4.3 but not for my iPod Touch 4 with iOS5.1.1.
I am newbie to iphone technology,right now i m working with an application where i need to implement push notification.
I followed the link :
http://mobiforge.com/developing/story/programming-apple-push-notification-services#comment-7850
Also,Used the following code :
NSLog(#"Registering for push notifications...");
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *str = [NSString stringWithFormat:#"Device Token=%#",deviceToken];
NSLog(str);
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
NSString *str = [NSString stringWithFormat: #"Error: %#", err];
NSLog(str);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
for (id key in userInfo)
{
NSLog(#"key: %#, value: %#", key, [userInfo objectForKey:key]);
}
}
Thing is,when i run the program,i should get device token in the debugger window,as per the code,instead i am getting error like :
" Error in registration. Error: Error
Domain=NSCocoaErrorDomain Code=3010
"remote notifications are not
supported in the simulator"
UserInfo=0x6e055a0
{NSLocalizedDescription=remote
notifications are not supported in the
simulator} "
How should i solve this problem?
Kindly help me out.
Thank you.
Because the simulator doesn't support it... In the example it displays the device identifier in the console. The console is displaying the feedback from the device. It isn't the console that is getting the information, but the device sending the information back. So, just because the console displays information on your Mac doesn't mean your Mac is capable of directly getting that information. Sometimes it must be sent by the device. Try running it on a device.
The error message is self explaining, you should try to debug the app on real device not on the simulator, as push notifications are not supported to be received on the simulators.