web-dev-qa-db-ja.com

コード更新後にiOS 8対応デバイスがプッシュ通知を受信しない

私は最近、テスト用のiPhoneの1つをiOS 8にアップグレードし、次にプッシュ登録コードを以下のようにアップグレードしました(xCode 6を使用)

-(BOOL)hasNotificationsEnabled {

    NSString *iOSversion = [[UIDevice currentDevice] systemVersion];
    NSString *prefix = [[iOSversion componentsSeparatedByString:@"."] firstObject];
    float versionVal = [prefix floatValue];


    if (versionVal >= 8)
    {

        NSLog(@"%@", [[UIApplication sharedApplication]  currentUserNotificationSettings]);
        //The output of this log shows that the app is registered for Push so should receive them

        if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {

            return YES;

        }

    }
    else
    {
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (types != UIRemoteNotificationTypeNone){
            return YES;
        }

    }

    return NO;
}

-(void)registerForPUSHNotifications {

    NSString *iOSversion = [[UIDevice currentDevice] systemVersion];
    NSString *prefix = [[iOSversion componentsSeparatedByString:@"."] firstObject];
    float versionVal = [prefix floatValue];


    if (versionVal >= 8)
    {


            //for iOS8
        UIUserNotificationSettings *settings =
        [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
         UIUserNotificationTypeBadge |
         UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];


    }
    else
    {

            [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];


    }
}

このアップグレードと[[UIApplication sharedApplication] currentUserNotificationSettings]がプッシュがデバイスに対して有効になっていることを示すという事実にもかかわらず、私はプッシュ通知を受信して​​いません。

私はParseを使用して、それらが関係する限り、本ですべてを実行しています( https://parse.com/tutorials/ios-Push-notifications )。

誰も同じ問題を経験していますか?私が行方不明になっているかもしれない何か他のものはありますか?

31
Zigglzworth

プッシュ通知の登録方法はiOS 8で変更されました。以下は、iOS 9までのすべてのバージョンのコードです。

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

以下のコードを使用して、プッシュ通知が有効になっているかどうかを確認する場合:

- (BOOL) pushNotificationOnOrOff
{
    if ([UIApplication instancesRespondToSelector:@selector(isRegisteredForRemoteNotifications)]) {
        return ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]);
    } else {
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        return (types & UIRemoteNotificationTypeAlert);
    }
}

#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application   didRegisterUserNotificationSettings:   (UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString   *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
    //handle the actions
    if ([identifier isEqualToString:@"declineAction"]){
    }
    else if ([identifier isEqualToString:@"answerAction"]){
    }
}
#endif

上記のコードはXcode 6以降でのみ実行されます...

67
pankaj

.mファイルの先頭にこの行を追加します

 #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

1)IOS8のプッシュ通知に登録するときに条件を設定する必要があります。このコードをアプリケーションdid finish launchに追加します。

if(IS_IOS_8_OR_LATER) {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: (UIRemoteNotificationTypeBadge
                                                                                         |UIRemoteNotificationTypeSound
                                                                                       |UIRemoteNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
 } else {
    //register to receive notifications
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
 }

2)次に、IOS8にこのメソッドを追加します

#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:   (UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString   *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
    //handle the actions
    if ([identifier isEqualToString:@"declineAction"]){
    }
    else if ([identifier isEqualToString:@"answerAction"]){
    }
}
#endif

次に、通知デリゲートメソッドが呼び出されます。これがあなたを助けることを願っています!!!

12
Nits007ak

ランタイムおよび古いコンパイラセーフオプション...古いxcode(5.0以前)で実行する場合

    // changes of API in iOS 8.0
- (void) registerForPushNotification
{
    NSLog(@"registerForPushNotification");
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 //__IPHONE_8_0 is not defined in old xcode (==0). Then use 80000

        NSLog(@"registerForPushNotification: For iOS >= 8.0");

        [[UIApplication sharedApplication] registerUserNotificationSettings:
            [UIUserNotificationSettings settingsForTypes:
                (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
                                              categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
    } else {
        NSLog(@"registerForPushNotification: For iOS < 8.0");
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    }
}
4
karim
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    // For iOS 8

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

 [[UIApplication sharedApplication] registerForRemoteNotifications];
} 

else 
{
    // For iOS < 8
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

この後:

  1. プッシュ通知を持たないアプリをデバイスから削除します。
  2. デバイスを完全にオフにして、再びオンにします。
  3. 設定に移動し、1日先の日時を設定します
  4. デバイスの電源を再度オフにして、元に戻す
  5. アプリを再度インストールすると、最初にインストールしたときと同じように通知を求められます。
  6. 自動的に設定されない場合は、日付/時刻を再度リセットします。

これにより、通知設定がリセットされます。アプリを再インストールすると、すべて正常に動作します:)

4
Muzammil

IOS 8と古いバージョンの両方がある場合は、これを試してください:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    // For iOS 8
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
    // For iOS < 8
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

Appleの開発サイトから:

IUserNotificationSettings

UIUserNotificationSettingsオブジェクトは、アプリがユーザーに表示できる通知の種類をカプセル化します。ローカルまたはプッシュ通知と組み合わせて可視または可聴アラートを使用するアプリは、使用するアラートのタイプを登録する必要があります。 UIKitは、提供する情報とユーザーの設定を関連付けて、アプリで使用できるアラートの種類を決定します。

このクラスを使用して、初期登録要求をカプセル化し、要求結果を表示します。このクラスのインスタンスを作成し、好みの設定を指定したら、UIApplicationクラスのregisterUserNotificationSettings:メソッドを呼び出して、これらの設定を登録します。リクエストをユーザー設定と照合した後、アプリは結果をアプリデリゲートのapplication:didRegisterUserNotificationSettings:メソッドに配信します。そのメソッドに渡されるオブジェクトは、アプリが使用を許可される通知のタイプを指定します。

アプリのアラートタイプを登録するだけでなく、このクラスを使用して、ローカル通知またはプッシュ通知とともに表示するカスタムアクションのグループを登録することもできます。カスタムアクションは、通知に応じてアプリが実行できる即時タスクを表します。アクションのグループを定義し、グループ全体を特定の通知に関連付けます。対応するアラートが表示されると、指定したアクションごとにボタンが追加されます。ユーザーがいずれかのアクションのボタンをタップすると、システムがアプリを起動し、アプリのデリゲートのapplication:handleActionWithIdentifier:forRemoteNotification:completionHandler:またはapplication:handleActionWithIdentifier:forLocalNotification:completionHandler:メソッドを呼び出します。これらのメソッドを使用して、要求されたアクションを実行します。

1

非常に簡単です:

xcode6のプロジェクトのdidFinishLaunchingWithOptionsに次の行を追加します

 [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
 [[UIApplication sharedApplication] registerForRemoteNotifications];
1
Divya Bharathi

設定オブジェクトのタイプ情報を取得しようとしましたが、typesプロパティは基本的にビットマスクであることがわかりました。情報を抽出する方法は次のとおりです。

私の例はSwift and> = iOS 8.0です。

    let settings = UIApplication.sharedApplication().currentUserNotificationSettings()

    if settings.types.rawValue & UIUserNotificationType.Alert.rawValue == UIUserNotificationType.Alert.rawValue
    {
       // can receive alert!
    }
    else
    {
       // if the user is not even able to receive alerts, show him a hint on how to reenable notifications in system settings
    }

    if settings.types.rawValue & UIUserNotificationType.Badge.rawValue == UIUserNotificationType.Badge.rawValue
    {
        // can receive badge!
    }
    if settings.types.rawValue & UIUserNotificationType.Sound.rawValue == UIUserNotificationType.Sound.rawValue
    {
        // can receive sound!
    }
0
Raphael
The code below resolved:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    UIUserNotificationType types;
    types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
    if (types & UIUserNotificationTypeAlert)
        pushEnabled=YES;
    else
        pushEnabled=NO;
}
else
{
    UIRemoteNotificationType types;
    types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types & UIRemoteNotificationTypeAlert)
       pushEnabled=YES;
    else
       pushEnabled=NO;

}
0
Adelmo Júnior