无法推送推送通知

时间:2017-06-17 08:35:44

标签: ios objective-c firebase apple-push-notifications

我正在尝试在我的应用程序中实现推送通知。我正在使用APNs身份验证密钥。到目前为止我找到的每个指南都有弃用方法或者在Swift中。我对像this one这样的指南很感兴趣,但是在目标C中。

到目前为止,我在AppDelegate.m中做了什么:

@import Firebase;

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

    [FIRApp configure];

    NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
        [self application:application didReceiveRemoteNotification:userInfo];
    }

    UIUserNotificationType allNotificationTypes =
    (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

    return YES;
}


- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {



    // Perform different operations depending on the scenario
    if (application.applicationState == UIApplicationStateInactive) {

        // Opening the app
        NSLog(@"Firebase opening app");


    }
    else if (application.applicationState == UIApplicationStateBackground) {

        // Coming from background
        NSLog(@"Firebase background");

    }
    else {

        // Active
        NSLog(@"Firebase active");
    }

}

不幸的是,当我从firebase发送消息时,我什么都没得到,xcode控制台没有输出,也没有通知。 如果你能指出我正确的方向,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

请使用此方法:

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

而不是:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
 //Do something
}

<强>更新

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

    [FIRApp configure];

    UIUserNotificationType allNotificationTypes =
    (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
      {
        #ifdef __IPHONE_8_0
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert
                                                                                             | UIUserNotificationTypeBadge
                                                                                             | UIUserNotificationTypeSound) categories:nil];
        [application registerUserNotificationSettings:settings];
        #endif
       }
    else
      {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
      }
    return YES;
}




-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{

    [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
}

答案 1 :(得分:0)

您既不连接数据库也不实现firebase apn令牌。为什么不像这里firebase example那样实现它。 你有没有设置plist和POD库?在上面的代码示例中,版本8/9的iOS部分只有不推荐使用的代码 - 意味着您可以忽略这些代码行。

相关问题