在应用委托之外注册远程通知
Posted
技术标签:
【中文标题】在应用委托之外注册远程通知【英文标题】:Register for remote notifications outside of app delegate 【发布时间】:2013-06-19 04:26:05 【问题描述】:到目前为止,我所看到的一切都表明我将在我的AppDelegate
中设置推送通知警报。但是,我的应用程序要求用户完成注册过程,我不想询问用户是否愿意接收推送通知,除非用户已经到达注册过程完成后出现的viewController
。
除了我的应用程序委托之外,我能否将其中一些代码放入视图控制器的 viewDidLoad
方法中?我是否需要在我的应用委托中保留这两个底部方法“didRegisterForRemoteNotificationsWithDeviceToken
”和“didReceiveRemoteNotification
”,还是应该将它们移动到我尝试注册远程通知的任何地方?
我正在使用以下代码块在我的应用中注册推送通知:
在我的应用委托的 didFinishLaunchingWithOptions 方法中:
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
UIRemoteNotificationTypeAlert|
UIRemoteNotificationTypeSound];
在我的应用委托中添加的方法:
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
// Store the deviceToken
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
//handle push notification
我访问过的资源表明这块代码
【问题讨论】:
【参考方案1】:您可以随时进行注册调用 - 最好仅在您在应用中知道您希望用户接收推送通知时才这样做。
这两个应用程序委托回调必须在您的应用程序委托中,因为您在应用程序委托上注册通知类型而您只有一个。我建议创建一个应用程序委托方法来调用,然后进行注册,您可以通过[[UIApplication sharedApplication] delegate]
从您的视图控制器调用它(将该调用的结果转换为您的应用程序委托类)。
【讨论】:
【参考方案2】:这个答案是对 Kendall Helmstetter Gelner 的“投票”回复(Kendall 的回答有 0 票赞成,但对我来说效果很好,而且我没有足够的分数来投票赞成答案)。按照 Kendall 的建议,我的视图控制器 CMRootViewController.m ->
#pragma mark - push notificaiton
-(void)registerToReceivePushNotification
// Register for push notifications
UIApplication* application =[UIApplication sharedApplication];
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
两个应用程序委托回调在我的应用程序委托 CMAppDelegate.m ->
// handle user accepted push notification, update parse
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:newDeviceToken];
// enable future push to deviceId
NSUUID *identifierForVendor = [[UIDevice currentDevice] identifierForVendor];
NSString* deviceId = [identifierForVendor UUIDString];
[currentInstallation setObject:deviceId forKey:@"deviceId"];
[currentInstallation saveInBackground];
// handle push notification arrives when app is open
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
[PFPush handlePush:userInfo];
谢谢肯德尔。
【讨论】:
【参考方案3】:最好的方法是,在您的应用程序委托方法中处理删除通知,使用NSNotificationCenter
发送通知
[[NSNotificationCenter defaultCenter] postNotification:@"remoteNotification" withObject:whateverYouWantHere];
然后使用NSNotificationCenter
添加任何感兴趣的UIViewControllers 作为remoteNotification
通知的观察者。
【讨论】:
【参考方案4】:SWIFT 3 及更高版本
从 AppDelegate 外部调用推送通知
import UserNotifications
class HomeViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
let application = UIApplication.shared
registerPushNotification(application)
func registerPushNotification(_ application: UIApplication)
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) (granted, error) in
if granted
print("Notification: Granted")
else
print("Notification: not granted")
application.registerForRemoteNotifications()
extension HomeViewController
// Called when APNs has assigned the device a unique token
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
// Convert token to string
let deviceTokenString = deviceToken.reduce("", $0 + String(format: "%02X", $1))
// Print it to console
print("APNs device token: \(deviceTokenString)")
// Persist it in your backend in case it's new
// Called when APNs failed to register the device for push notifications
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
// Print the error to console (you should alert the user that registration failed)
print("APNs registration failed: \(error)")
// Push notification received
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any])
// Print notification payload data
print("Push notification received: \(data)")
【讨论】:
扩展功能无效。扩展中的自动呼叫功能如何实现? 此方法不注册推送通知,它只请求“在本地和远程通知发送到用户设备时与用户交互”,应该在“安排任何本地通知之前和之前调用”向 Apple 推送通知服务注册”。来源:developer.apple.com/documentation/usernotifications/…以上是关于在应用委托之外注册远程通知的主要内容,如果未能解决你的问题,请参考以下文章