若是收到推送时,App 在前台运行,那么:ios
//UNUserNotificationCenterDelegate
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}复制代码
iOS 使用推送须要 配置推送证书,若是你配过了,仍然收不到推送,建议再配一遍。笔者曾经屡次远程控制手把手给其余开发者配置证书,结果全都从新配一遍就能收到推送了。服务器
Xcode 8 这里打开。
app
推送时开发环境、发布环境一一对应。环境不对应,收不到。ide
要在 Apple Developer Center 把你的测试设备加入到 Device 里面。测试
一台手机能收到,另外一台不能收到。要把你的另外一台测试设备也加入到 Apple Deveice 里面。。。fetch
高峰时段你再等几秒就收到了。ui
Apple 服务器宕机了,并非全部的设备都收不到推送了,而是新的设备没法成功注册推送服务了。spa
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
}复制代码
在该方法中,没法返回有效的 deviceToken 值了。等两天就行了。code
打包,安装,测试。便可。cdn
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];复制代码
推送时 badge 参数设置: +n自增、-n自减、n固定值。
自增、自减通常第三方推送服务支持才有。
Background Remote Notification
App 杀死啥代码都不能执行。iOS 10 你能够试试 Notification Service Extension,详情见 这篇文章 的 Service Extension 部分。
哥哥手机左侧静音键麻烦拨上来。
推送时 sound 字段设置为:default
1.声音文件拖拽并拷贝并生成索引到工程任意位置(在 Xcode 里拖拽)
2.推送时 sound 字段设置为:name.mp3。须要名字后缀彻底同样。
拖拽文件时没有选择拷贝,连线测试有声音,拔线没声音,声音文件在电脑里,拔线后缺失。从工程目录删除该文件,再从新添加再 build。
要点推送横幅、通知中心条目进入 App 才能收到。
解决方法:再发一条透传消息。只处理消息,不处理推送。不论点哪里进的 App,都能保证该事件被处理。
iOS 9 的 bug。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
}复制代码
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
}复制代码
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
}复制代码
//UNUserNotificationCenterDelegate
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary *userInfo = response.notification.request.content.userInfo;
}复制代码
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];复制代码
推送时 badge 参数为 0,再调用 Q 22 致使通知中心没有发生变化,此时应设为1,再设为0:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];复制代码
先获取用户推送权限设置状况:
// iOS 8 before
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
// iOS 8~9
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
UIUserNotificationType type = settings.types;
// iOS 10
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
}复制代码
若是没开,跳转到 设置 - yourApp 页面让用户设置:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];复制代码
// iOS 8 before
UIRemoteNotificationType type = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:type];
// iOS 8~9
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[application registerUserNotificationSettings:setting];
// iOS 10
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];复制代码
由于他是极光推送的可耐程序猿葛格^^。