سؤال

My app is able to successfully register for push notifications but I would like to move the pop-up alert to a different area of the app

If I implement the same code from AppDelegate.m to a different screen, Other.m, of my app, it never registers:

-(void)buttonTapped {
    // Register for Push Notifications
    UIRemoteNotificationType notifyTypes = (UIRemoteNotificationTypeAlert |                     UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeBadge);

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notifyTypes];
}


#pragma mark - APNS Functions
-(void)application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken {
    NSLog(@"didRegisterForRemoteNotifications - 1");
}


-(void)application:application didFailToRegisterForRemoteNotificationsWithError:error {
    NSLog(@"didFailToRegisterForRemoteNotifications");
}

If I enable didRegisterForRemoteNotificationsWithDeviceToken in my AppDelegate.m, the AppDelegate.m instance of the method gets called from my Other.m, but that is not how I want this to work.

Any ideas or suggestions would be appreciated.

هل كانت مفيدة؟

المحلول

Yes, of course.

You can register wherever you want with using

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

but didRegisterForRemoteNotificationsWithDeviceToken:deviceToken is only available in AppDelegate

نصائح أخرى

-(void)application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken

This is the protocol method of the UIApplicationDelegate protocol, so it will be triggered on [UIApplication sharedApplication].delegate. This is the AppDelegate by default.

The accepted answer was deprecated in iOS 8.0. Following Swift code works for iOS 9.0:

let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top