未收到iOS 12 FCM推送通知

时间:2019-04-22 11:00:32

标签: ios firebase push-notification firebase-cloud-messaging

设备具有iOS 12或iOS11。有时他们会收到FCM推送通知,但有时并没有连续收到FCM推送通知。

在卸载并重新安装应用程序并获取新设备/ fcm令牌时,他们开始获得FCM推送通知。没有任何代码更改。

我不知道代码有什么问题? 谁能帮助我解决这个问题,以及为什么用户在重新安装应用后再次收到通知?

我的代码配置

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        [FIRApp configure];

            [UNUserNotificationCenter currentNotificationCenter].delegate = self;
                UNAuthorizationOptions authOptions =
                UNAuthorizationOptionAlert
                | UNAuthorizationOptionSound
                | UNAuthorizationOptionBadge;
                [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
                }];

        [application registerForRemoteNotifications];

            [FIRMessaging messaging].delegate = self;
            UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

                if (localNotif)
                {
                    NSUserDefaults *userDefaultLocalNotif = [NSUserDefaults standardUserDefaults];
                    [userDefaultLocalNotif setObject:localNotif forKey:@"localNotif"];
                    [userDefaultLocalNotif synchronize];
                }

        return TRUE;

        }


        - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken 
{

        NSLog(@"FCM registration token 1: %@", fcmToken);

        // TODO: If necessary send token to application server.
        // Note: This callback is fired at each app startup and whenever a new token is generated.
    }

    - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        //NSString *deviceTokenString = [NSString stringWithFormat:@"%@",deviceToken];
        //NSLog(@"deviceTokenString : %@",deviceTokenString);

        [FIRMessaging messaging].APNSToken = deviceToken;

        //NSLog(@"FCM registration token 2: %@", [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding]);

    }


    // [START receive_message]
    - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
    {

        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification

        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
        /*
         // Print message ID.
         if (userInfo[kGCMMessageIDKey]) {
         NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
         }
         */
        // Print full message.
        NSLog(@"Userinfo didReceiveRemoteNotification 1 %@",userInfo);


    }

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 
{
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification

        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

        /*
         // Print message ID.
         if (userInfo[kGCMMessageIDKey]) {
         NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
         }
         */
        // Print full message.

        NSLog(@"Userinfo didReceiveRemoteNotification 2 %@",userInfo);

        [self getPushNotificationDetails:userInfo];

        completionHandler(UIBackgroundFetchResultNewData);
    }
    // [END receive_message]

    // [START ios_10_message_handling]
    // Receive displayed notifications for iOS 10 devices.
    //#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    // Handle incoming notification messages while app is in the foreground.

    -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{

        //Called when a notification is delivered to a foreground app.
        NSLog(@"Userinfo willPresentNotification%@",notification.request.content.userInfo);

        // Print message ID.
        /*
         NSDictionary *userInfo = notification.request.content.userInfo;
         if (userInfo[kGCMMessageIDKey]) {
         NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
         }
         */

        // PSM Anks use when we want to redirect user to Notification page when app is open and user on any view of application
        //[self getPushNotificationDetails:notification.request.content.userInfo];

        completionHandler(UNNotificationPresentationOptionAlert);

    }

    -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
{

        //Called to let your app know which action was selected by the user for a given notification.
        NSLog(@"Userinfo didReceiveNotificationResponse%@",response.notification.request.content.userInfo);

        // Print message ID.
        /*
         NSDictionary *userInfo = notification.request.content.userInfo;
         if (userInfo[kGCMMessageIDKey]) {
         NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
         }
         */

        [self getPushNotificationDetails:response.notification.request.content.userInfo];
    }

0 个答案:

没有答案