Reputation: 2880
I have built up my app to receive Firebase Notifications. I tested it through FCM Console and device got notification. However when I test from testflight device didnt receive any notification but when I check the payload it showed success.
1. Uploaded the production .p12 certificate in firebase.
2. Capabilities -> Background Modes = ON with Remote Notifications = true;
PushNotifications = set to 'true'
3. In Info.plist FirebaseAppDelegateProxyEnabled = YES ,
FirebaseScreenReportingEnable = NO
Here is my code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([UNUserNotificationCenter class] != nil) {
// iOS 10 or later
// For iOS 10 display notification (sent via APNS)
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
} else {
// iOS 10 notifications aren't available; fall back to iOS 8-9 notifications.
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
}
[application registerForRemoteNotifications];
[FIRApp configure];
[self connectToFcm];
[FIRMessaging messaging].delegate = self;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) name:kFIRInstanceIDTokenRefreshNotification object:nil];
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
[[FIRMessaging messaging] setAPNSToken:deviceToken type:FIRMessagingAPNSTokenTypeSandbox];
[FIRMessaging messaging].APNSToken = deviceToken;
}
- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
self.DeviceId=fcmToken;
// Notify about received token.
NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
[[NSNotificationCenter defaultCenter] postNotificationName:
@"FCMToken" object:nil userInfo:dataDict];
}
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
NSLog(@"%@", remoteMessage.appData);
}
- (void)tokenRefreshNotification:(NSNotification *)notification {
[[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,
NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Error fetching remote instance ID: %@", error);
} else {
NSString *refreshedToken = result.token;
self.DeviceId = refreshedToken;
NSLog(@"InstanceID token: %@", refreshedToken);
NSLog(@"Remote instance ID token: %@", result.token);
}
}];
}
- (void)connectToFcm {
[[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,
NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Error fetching remote instance ID: %@", error);
} else {
NSLog(@"Remote instance ID token: %@", result.token);
}
}];
}
Tested it via restclient and got this result
{
"multicast_id": 4659854677338425000,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1569222922386579%12eeef7ecccfb49c"
}
]
}
Why my device is not showing up notification even though it is success ?
Any ideas/help would be highly appreciated.
Upvotes: 1
Views: 3445
Reputation: 31
I spent many hours with the same issue. Then I discovered when I sent test message using fcm notification console I received the notification on the iphone in the notification center. However in my php app server I never got it. I always get android. Then I read Postman Send FCM and I got the same results like fcm console. Then I discovered the issue was that my code was using:
$fields = array(
'registration_ids' => $registration_ids,
'data' => $message,
);
I then changed it to match console and postman and it worked!:
$fields =
[
'to' => 'TOKEN',
'notification' => [
'title' => "It Works",
'body' => "12 hours later"
]
];
I turns out whenever I used this custom array that it never gets through to IOS but does to Android. Further now on android the message goes into notification center of app instead of inside my app message center. Hope this helps you also.
Upvotes: 2
Reputation: 11
Have you done this setup "Create the authentication key" correctly ?
https://firebase.google.com/docs/cloud-messaging/ios/certs
Upvotes: 1