LoginSignup
11
9

More than 5 years have passed since last update.

Push通知用のデバイストークンが取得できない場合の対処法

Last updated at Posted at 2017-03-01

iOSでPush通知を行うアプリを開発した際、
デバイストークンが渡される以下メソッドが呼ばれない場合の確認事項

Objective-C
- (void)application:(UIApplication *)application 
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //デバイストークン取得後の処理
}
Swift
func application( application: UIApplication, 
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {
    //デバイストークン取得後の処理
}

確認事項

  1. target設定のCapabilities > Push NotificationsがONになっていることを確認する。
    スクリーンショット 2017-03-01 23.25.20.png

  2. iOS8以降の場合、registerForRemoteNotificationsをコールしているか確認する。

Objective-C
- (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIUserNotificationType types =
                        UIUserNotificationTypeBadge|
                        UIUserNotificationTypeSound|
                        UIUserNotificationTypeAlert;

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    [application registerUserNotificationSettings:settings];
    return YES;
}

- (void)application:(UIApplication *)application 
    didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application 
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //デバイストークン取得後の処理
}

- (void)application:(UIApplication *)application
    didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    // エラー処理
}
Swift
func application( application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary? ) -> Bool {
    let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
    application.registerUserNotificationSettings(settings)
    return true
}

func application( application: UIApplication,
     didRegisterUserNotificationSettings notificationSettings:UIUserNotificationSettings) {     
     application.registerForRemoteNotifications()
}

func application( application: UIApplication,
     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    //デバイストークン取得後の処理
}

func application( application: UIApplication!, 
    didFailToRegisterForRemoteNotificationsWithError error: NSError! ) {
    // エラー処理
}

参考

11
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9