发送推送后
1.如果此时App已经被杀死、或在app后台时间过长,已断开与个推长链接
则发送的是离线推送,通过Apns发送通知,点击通知栏内的消息,app进入前台,执行:
- (void)GeTuiSdkDidReceiveNotification:(NSDictionary *)userInfo notificationCenter:(UNUserNotificationCenter *)center response:(UNNotificationResponse *)response fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
同时,不会走个推通知展示的方法
- (void)GeTuiSdkNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification completionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
2.如果此时App在后台,但是未断开与个推的长链接,即刚刚退出app返回桌面时
则发送的是个推的透传消息,此时,正常情况下,苹果通知栏并不会展示任何通知出来,但是app内部会收到个推的回调方法:
/// 收到透传消息,cid在线,走此方法
/// @param userInfo 推送消息内容
/// @param fromGetui YES: 个推通道 NO:苹果apns通道
/// @param offLine 是否是离线消息,YES.是离线消息
/// @param appId 应用的appId
/// @param taskId 推送消息的任务id
/// @param msgId 推送消息的messageid
/// @param completionHandler 用来在后台状态下进行操作(通过苹果apns通道的消息 才有此参数值)
- (void)GeTuiSdkDidReceiveSlience:(NSDictionary *)userInfo fromGetui:(BOOL)fromGetui offLine:(BOOL)offLine appId:(NSString *)appId taskId:(NSString *)taskId msgId:(NSString *)msgId fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
那么一般情况,用户无法收到(看到)这条推送,所以我们需要在收到透传消息后,发送一条本地通知,用来通知用户:
- (void)postLocalNotification:(NSDictionary*)userInfo{
/** 清除之前的通知 */
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSString *gmid = nil;
UILocalNotification *localNotify = [[UILocalNotification alloc] init];
/** 设置通知触发的时间在几秒后 */
NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotify.fireDate = pushDate;
localNotify.timeZone = [NSTimeZone defaultTimeZone];
localNotify.repeatInterval = kCFCalendarUnitDay;
/** 设置通知的音效(只有真机有效) */
localNotify.soundName = UILocalNotificationDefaultSoundName;
/** 推送内容 */
localNotify.alertBody = userInfo[@"payload"];
/** 获取通知消息数组,添加一次通知数量(因为此时用户还没点击通知,只是收到通知) */
NSArray *notifyArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
int count = (int) [notifyArray count];
localNotify.applicationIconBadgeNumber = count + 1;
// 备注:点击统计需要
if (gmid != nil) {
NSDictionary *userInfoDict = @{@"_gmid_":gmid};
localNotify.userInfo = userInfoDict;
}
/** 注册本地通知 */
[[UIApplication sharedApplication] scheduleLocalNotification:localNotify];
}
如用户此时点击了此条通知,则会继续调用个推通知展示的代理方法:
- (void)GeTuiSdkNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification completionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
// completionHandler(UNNotificationPresentationOptionNone); //若不显示通知,则无法点击通知
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
3.如果此时App在前台
则发送的消息逻辑与第二种情况一致,也是走个推的透传消息,但是,有一点区别,收到透传消息,app发送本地通知后,会自动调用个推的通知展示方法,即使用户没有点击这条推送:
- (void)GeTuiSdkNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification completionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
记录一下:iOS点击home退出前台后,app会在5秒内终止代码运行,包括个推的透传消息也传递不过来,但是,此时app与个推的链接还是存在的,但是个推的代理方法无法运行。。。而由于链接还在,apns消息也也不会传递过来,这个断线过程大概会持续2分钟左右,
网友评论