1. ホーム
  2. ios

[解決済み] ios8ではdidRegisterForRemoteNotificationsWithDeviceTokenは呼ばれないが、didRegister...Settingsは呼ばれる。

2022-02-10 08:13:59

質問

私は このスレッド しかし、その方法は didRegisterForRemoteNotificationsWithDeviceToken はまだ呼び出されていません。

ドキュメントには :

のregisterForRemoteNotificationsメソッドを呼び出した後、そのメソッドを実行します。 UIApplicationオブジェクトは、デバイスが起動するとこのメソッドを呼び出します。 登録が正常に完了する

didRegisterUser はよく表示されるが、そうでない did register notif .

以下は、AppDelegateのコードです(アプリのバージョンは8.1です)。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //register notif
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];


    return YES;
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
    NSLog(@"didRegisterUser");
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"error here : %@", error);//not called
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    /*
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    currentInstallation.channels = @[ @"global" ];
    [currentInstallation saveInBackground];
     */
    NSLog(@"did register notif");//not called
}

また、info.plistにbackground mode ->リモート通知も入れています。

解決方法は?

あなたのコードは正しいようです。ちょっとした改善点として、didRegisterUserNotificationSettingsメソッドを以下のように記述することができます。

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    if (notificationSettings.types != UIUserNotificationTypeNone) {
        NSLog(@"didRegisterUser");
        [application registerForRemoteNotifications];
    }
}

設定に問題があり、APN登録に失敗している可能性があります。

  1. Provisioning Profile に aps-environment の項目が含まれていることを確認します。

  2. Provisioning Profile に一意の App Identifier("*"を含まない文字列)が設定されていることを確認します。また、Info.plistで、この正確な識別子をバンドル識別子として使用する必要があります。

  3. 最初のインストール後にPush機能を拒否した可能性があります。この場合、アプリ内Pushアラートは二度と表示されず、設定アプリでPushを再度有効にする必要があります。

  4. 別の端末で試してみてください。