Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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! ) {
    // エラー処理
}

参考

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away