收到推送通知
Posted
技术标签:
【中文标题】收到推送通知【英文标题】:Receipt of Push Notification 【发布时间】:2016-01-25 19:00:02 【问题描述】:无论应用程序处于何种状态(前台、后台、关闭),当push notification
已发送到手机时,我正在尝试将收据发送回网站。我已经附加了我的代码,iGotIt 只是对服务器的https
调用,不会被调用。我需要做什么才能让应用在收到push notification
时执行某些操作?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[application registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeSound |
UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
else
[application registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)];
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
NSString *messagePre = nil;
NSString *message = nil;
id alert = [userInfo objectForKey:@"aps"];
if ([alert isKindOfClass:[NSString class]])
messagePre = alert;
else if ([alert isKindOfClass:[NSDictionary class]])
messagePre = [alert objectForKey:@"alert"];
NSArray *messageSplit = [messagePre componentsSeparatedByString:@"|"];
serviceID = [messageSplit objectAtIndex:0];
message =[messageSplit objectAtIndex:1];
UIAlertView *alertRC = [[UIAlertView alloc]initWithTitle: @"New Road Call"
message: message
delegate: self
cancelButtonTitle:nil
otherButtonTitles:@"View",nil];
[alertRC show];
//Accept push notification when app is not open
if (userInfo)
[self iGotIt];
return YES;
提前感谢您提供的任何帮助
【问题讨论】:
【参考方案1】:如果用户通过点击收到的推送通知打开您的应用程序,didFinishLaunchingWithOptions:
是正确的查看方法。
但是,如果用户已经在应用程序内,或者应用程序在接收推送通知时处于后台,则将调用didReceiveRemoteNotification:fetchCompletionHandler:
(documentation)。关于主题here 的更多讨论。请记住在有效负载中设置content-available=1
,并在您的应用程序的功能中勾选远程通知。
对于您的应用被用户强制退出的最后一种情况,不幸的是,这并不是那么简单。这可以使用PushKit 实现,并且该主题也已在here 进行过讨论。
【讨论】:
以上是关于收到推送通知的主要内容,如果未能解决你的问题,请参考以下文章