Not receiving Push Notifications Using firebase an

2019-03-03 17:10发布

问题:

I am trying to use Firebase for push notification. i have successfully installed the Firebase And Firebase messaging via Cocoapods.

I used the below code

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [FIRApp configure];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshCallback:) name:kFIRInstanceIDTokenRefreshNotification object:nil];


    UIUserNotificationType allNotificationsType = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationsType categories:nil];
    [application registerUserNotificationSettings:settings];

    [application isRegisteredForRemoteNotifications];

    return YES;
}


 -(void)applicationDidEnterBackground:(UIApplication *)application {

    [[FIRMessaging messaging] disconnect];
    NSLog(@"Disconnect from FCM");
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
     [self connectToFirebase];
}

- (void) application:(UIApplication *) application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:( void (^)(UIBackgroundFetchResult))completionHandler{

    // Pring The Message Id
    NSLog(@"Mesage Id : %@",userInfo[@"gcm.message_id"]);

    // Print Full Message
    NSLog(@"%@",userInfo);
}

#pragma mark -- Custom Firebase code


- (void)tokenRefreshCallback:(NSNotification *) notification{
    NSString *refreshedToken = [[FIRInstanceID instanceID] token];
    NSLog(@"IstanceID token: %@", refreshedToken);

    // Connect To FCM since connection may have failed when attempt before having a token
    [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"));
         }
     }];
}

When i run the above code using my iPhone connected with Xcode i am facing the following issues when i send a message from Firebase Console

1). when the app is in foreground (active), Log shows the below message

Mesage Id : (null){
    CustommData = "First Message";
    "collapse_key" = "abcd.iospush.FireBasePush";
    from = 555551391562;
    notification =     {
        badge = 4;
        body = "Test Message";
        e = 1;
        sound = default;
        sound2 = default;
    };
}

Notice Message Id is null.

2). My phone doesn't show any notification in Notification Centre whether App is in Foreground , Background or Closed

I want user to receive push notifications when App is in Background , Foreground or Closed

回答1:

You must call [[UIApplication sharedApplication] registerForRemoteNotifications]; to properly register for remote notifications.

Configuring Remote Notifications For Background Usage

Have you configured your app's background modes to accept remote notifications? You can do this by clicking: Project Name -> Capabilities -> Background Modes

Turn this on and then tick the box beside Remote Notifications as seen in the screenshot below.



回答2:

You should be calling (iOS 8+) registerForRemoteNotifications, not isRegisteredForRemoteNotifications.

Otherwise, you'll never get "high priority" messages from the Firebase server, delivered natively using APNS.

Please note that when you're running in XCode, you are running in sandbox mode, so be sure to register with the proper parameters.

Try sending it in "high priority" and see if your payload is in the proper APNS format:

{
    "aps" : {
        "alert" : "You got your emails.",
        "badge" : 9,
        "sound" : "bingbong.aiff"
    },
}

Note that your payload may have = instead of :, which is acceptable.



回答3:

you need to create an App ID in your developer account that has the push notification entitlement enabled. Luckily, Xcode has a simple way to do this. Go to App Settings -> Capabilities and flip the switch for Push Notifications to On. After some loading, it should look like this: push notifications

Also check p12 certificate is set for Firebase as it uses .p12 certificate for push notifications.