2017-06-17 69 views
0

我試圖在我的應用程序中實現推送通知。我正在使用APN認證密鑰。到目前爲止,我發現的每個指南都有不贊成使用的方法,或者它在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"); 
    } 

} 

不幸的是,當我從火力點發送一條消息我什麼也沒得到,沒有輸出在xcode控制檯中,並沒有通知。 如果您能指出我正確的方向,我將不勝感激。

回答

0

你不執行火力APN令牌既不是你連接到數據庫。爲什麼不只是像這裏執行它firebase example。 你甚至設置了plist和POD庫嗎?在上面的代碼示例中,對於版本8/9的iOS部分只有不推薦使用的代碼 - 意味着您可以忽略這些代碼行。

+0

正是我需要的。感謝隊友 – student

+0

歡迎您。我在自己的最後一個項目中實施了自己,並且100%的工作。順便說一句,不要忘記在firebase中上傳證書(僅適用於iOS,Android不需要它)。我仍然像你一樣在Objective-C上 - 喜歡它!格爾茨 –

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]; 
} 
+0

什麼都沒發生 – student

+0

您是否使用'didRegisterForRemoteNotificationsWithDeviceToken'方法? –

+0

不,我應該在哪裏以及如何使用它?你有什麼參考? – student