Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我可以在AppDelegate.m之外注册推送通知吗?_Ios_Iphone_Objective C_Apple Push Notifications - Fatal编程技术网

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