I have intergrated firebase push notification in my app. I had installed cocoapods fine and do all the code in appdelegate.m file for firebase messaging. Also created certificate and uploaded to firebase to my project, i have followed all the procedure correctly but when i run the app i got some errors and app do not run and shows error while linking. My code for firebase is this,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[FIRApp configure];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(tokenRefreshCallBack:) name:kFIRInstanceIDTokenRefreshNotification object:nil];
UIUserNotificationType type=(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
UIUserNotificationSettings *settings=[UIUserNotificationSettings settingsForTypes:type categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
return YES;
}
-(void)tokenRefreshCallBack:(NSNotification *)notification{
NSString *refreshToken=[[FIRInstanceID instanceID]token];
NSLog(#"Instance IDToken : %#",refreshToken);
[self connecttoFirebase];
}
-(void)connecttoFirebase{
[[FIRMessaging messaging]connectWithCompletion:^(NSError * _Nullable error){
if (error !=nil) {
NSLog(#"Unable to connect to FCM %#",error);
}else{
NSLog(#"Connected to FCM");
}
}];
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(#"Message ID : %#",userInfo[#"gcm.message_id"]);
NSLog(#"%#",userInfo);
}
enter image description here
enter image description here
Sir David East's answer (https://stackoverflow.com/a/38226058/3231194) :
Go to Build Settings > Other Linker Flags > on a new line $(inherited).
Do a Cmd+Ctrl+K for a clean, and then build.
I also want to add these steps, rather than Cmd+Ctrl+K, it should be:
Cmd+Shift+K. Then:
Cmd+AltOption+Shift+K
Finally, build it.
Related
all.
I want to ask you about push notification settings.
Environments:
MacOS[High Sierra10.13.3]
Xcode9.2(9C40b)
Devices
- iPhone6S[iOS10.3.3]
- iPhoneX [iOS11.2.6]
Language:
Cocos2d-x (Objective-c)
This problem occurs suddenly, and above methods are called before,
so basic settings must have been correctly done such as "Capabilities", "Provisioning Profiles".
And of course, network is connected to wifi.
So now, here is my code snippet.
- (BOOL)application: (UIApplication*)application didFinishLaunchingWithOptions: (NSDictionary*)launchOptions {
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
}];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
Also, I try this one.
- (BOOL)application: (UIApplication*)application didFinishLaunchingWithOptions: (NSDictionary*)launchOptions {
UIUserNotificationSettings* setting = [UIUserNotificationSettings settingsForTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories: nil];
[[UIApplication sharedApplication] registerUserNotificationSettings: setting];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
I can see push notification dialog when app is launched, push allow or deny button, requestAuthorizationWithOptions callback is invoked, but nothing happens.
I repeatedly uninstall and install app, but nothing changed.
Added(14/03/2018 18:53)
I create new test project and use the same bundle identifier and Provisioning Profiles.
Then, didRegisterForRemoteNotificationsWithDeviceToken is invoked correctly.
So, maybe configuration of Provisioning Profiles and AppID are ok, my project's setting is incorrect...
If the project is targeting iOS version 10.0+, Apple recommends utilizing the new method for asking for notification permissions:
requestAuthorizationWithOptions:completionHandler:
https://developer.apple.com/documentation/usernotifications/unusernotificationcenter/1649527-requestauthorizationwithoptions?language=objc
I am sure that all the configurations has no problem ,including Provisioning Profile and code, background mode and so on.It's weird that the old project could not called 'didRegisterForRemoteNotificationsWithDeviceToken' but the new project is called.two project has the same bundle ID, code and other configuration.
I am sure apple side has no problem because other's app can work normal and my new project do.so i suspect that the reason is that the old project did some configuration but i don't know.
the old project was made by Xcode5.
the new project was made by Xcode7.3.
could anyone tell me how to solve this problem? thanks a lot.
Here is my code in the AppDelegate (the iOS version is 8.0) :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings]
}else{
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
}
return YES;
}
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(#"deviceToken ==== %#", deviceToken); // not called
}
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(#"did Fail To Register For Remote Notifications With Error: %#", error); // not called
}
Hello I went through same problem.
Go to Project target settings and enable Push notification services from 'Capabilities' tab.
It will work.
We have integrated Urban SDK using these guidelines.
We have tried to check the push notification using ad hoc profile where we have set below values in AirshipConfig.plist
inProduction=YES
productionAppKey=OUR PRODUCTION KEY
productionAppSecret= OUR PRODUCTION SECRET
Please check the below code which we have implemented in the AppDelegate file of project.
-(void) applicationDidFinishLaunching: (UIApplication *)application
{
.
.
.
.
UAConfig *config = [UAConfig defaultConfig];
config.automaticSetupEnabled=NO;
[UAirship takeOff:config];
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
NSLog(#"------------REGISTER DEVICE------------: >= 8");
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
}
else
{
NSLog(#"------------REGISTER DEVICE------------: <8 ");
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
.
.
.
.
}
#pragma mark Remote Notification methods
-(void) application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken // Device Registration
{
[[UAirship push] appRegisteredForRemoteNotificationsWithDeviceToken:devToken];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[[UAirship push] appRegisteredUserNotificationSettings];
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(#"%#", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"NOTIFICATION------------ didReceiveRemoteNotification ------------ %#", userInfo);
[[UAirship push] appReceivedRemoteNotification:userInfo applicationState:application.applicationState];
}
When we are trying to send the notification to the registered device token, the server shows that device token as INACTIVE Device token.
Please show me what should I do for this.
So, first it looks like you are missing a few app delegate methods.
Urban Airship handles APNS registration. Remove your manual registration calls as UA will just override them, and instead, enable notifications with [UAirship push].userPushNotificationsEnabled = YES;. It should prompt the user to accept notifications, and then the Urban Airship channel should be opted in.
I have integrated apple push notification and facing a strange problem in my application. when I directly install the application via Xcode through usb connection then the device token is being generate stored in database correctly and push notification is working fine. but when I create IPA and install the app via created ipa in the same device then the device token is getting generated wrong and push notification is not working. Below is my code:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
application.applicationIconBadgeNumber = 0;
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings
*)notificationSettings {
[application registerForRemoteNotifications];
}
#endif
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData
*)deviceToken {
const unsigned *tokenData = deviceToken.bytes;
NSString *deviceTokenString = [NSString stringWithFormat:#"%08x%08x%08x%08x%08x%08x%08x%08x", ntohl(tokenData[0]),ntohl(tokenData[1]),ntohl(tokenData[2]),ntohl(tokenData[3]),ntohl(tokenData[4]),ntohl(tokenData[5]),ntohl(tokenData[6]),ntohl(tokenData[7])];
[[NSUserDefaults standardUserDefaults]setObject:deviceTokenString forKey:#"devicetoken"];
NSLog(#"Device Token = %#", deviceTokenString);
}
//Failed to Register for Remote Notifications
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(#"Error in registration. Error: %#", error);
}
The device Token depends on the certificate you signed your application with. If you install directly it is the dev-certificate, while when signing for AdHoc it is a distribution certificate. For push you need a corresponding distribution or development certificate packed on your server.
Been stuck on this forever, but could not get the following methods to be called. I am able to get the phone to ask for permission but after that it gets stuck.
- (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);
}
I have tried quite a few things including:
1) everything offered in the following stackoverflow post: why didRegisterForRemoteNotificationsWithDeviceToken is not called
2) Looking at this technical note from Apple: https://developer.apple.com/library/ios/technotes/tn2265/_index.html
3) This entire tutorial: http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 (so hopefully my certificates are all good)
4) (And yes I have internet connection)
Does anyone have any possible solutions? I have been rattling my brains out for the last 2-3 days and have already had to factory reset my phone twice, as well as change the date on my phone N^e number of times in order to get the notification popup to appear to test over and over again.
Would love any help! Thanks!
Here is what I am using to call... (tried a few other versions):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//other stuff not related
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
#ifdef __IPHONE_8_0
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
#endif
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
return YES;
}
iOS 8 introduced a change in the flow of registration that requires explicitly asking to register remote notifications after getting permission from the user.
You are calling this code?
[[UIApplication sharedApplication] registerUserNotificationSettings: settings];
And implementing this in your Application Delegate?
- (void) application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
That said, the old APIs are now deprecated but are still required if you plan to support iOS 7. To support both you can check to see if UIApplication responds to the new selector:
if ([[UIApplication sharedApplication] respondsToSelector:#SEL(registerUserNotificationSettings:)]) {
// iOS 8
[[UIApplication sharedApplication] registerUserNotificationSettings: settings];
// [[UIApplication sharedApplication] registerForRemoteNotifications]
// will be called in your UIApplicationDelegate callback
} else {
// iOS 7
[[UIApplication sharedApplication] registerForRemoteNotifications];
}