Ios 我可以在AppDelegate.m之外注册推送通知吗?
我的应用程序能够成功注册推送通知,但我想将弹出警报移动到应用程序的其他区域 如果我将相同的代码从Ios 我可以在AppDelegate.m之外注册推送通知吗?,ios,iphone,objective-c,apple-push-notifications,Ios,Iphone,Objective C,Apple Push Notifications,我的应用程序能够成功注册推送通知,但我想将弹出警报移动到应用程序的其他区域 如果我将相同的代码从AppDelegate.m实现到我的应用程序的另一个屏幕Other.m,它将不会注册: -(void)buttonTapped { // Register for Push Notifications UIRemoteNotificationType notifyTypes = (UIRemoteNotificationTypeAlert | U
AppDelegate.m
实现到我的应用程序的另一个屏幕Other.m
,它将不会注册:
-(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");
}
如果我在我的AppDelegate.m
中启用了didRegisterForRemotionTificationswithDeviceToken
,则会从我的Other.m
调用该方法的AppDelegate.m
实例,但我不希望这样做
如有任何意见或建议,将不胜感激 当然可以
您可以使用
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
但是DidRegisterForRemotionTificationswithDeviceToken:deviceToken仅在AppDelegate中可用
-(void)application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken
这是UIApplicationLegate协议的协议方法,因此它将在[UIApplication sharedApplication].委托上触发。默认情况下,这是AppDelegate。接受的答案在iOS 8.0中被弃用。以下Swift代码适用于iOS 9.0:
let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
Other.m是否有办法知道注册是否成功?您可以使用NSNotificationCenter在任何ViewController中触发remotenotification register successful或fail