/// 一般在在启动时注册通知,程序被杀死,点击通知后调用此程序
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // iOS8
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:setting];
}
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
// 这里添加处理代码
}
return YES;
}
/// 程序没有被杀死(处于前台或后台),点击通知后会调用此程序
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// 这里添加处理代码
}
- (IBAction)addLocalNotification {
// 1.创建一个本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 1.1.设置通知发出的时间
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// 1.2.设置通知内容
localNote.alertBody = @"这是一个推送这是一个推送";
// 1.3.设置锁屏时,字体下方显示的一个文字
localNote.alertAction = @"赶紧!!!!!";
localNote.hasAction = YES;
// 1.4.设置启动图片(通过通知打开的)
localNote.alertLaunchImage = @"../Documents/IMG_0024.jpg";
// 1.5.设置通过到来的声音
localNote.soundName = UILocalNotificationDefaultSoundName;
// 1.6.设置应用图标左上角显示的数字
localNote.applicationIconBadgeNumber = 999;
// 1.7.设置一些额外的信息
localNote.userInfo = @{@"qq" : @"704711253", @"msg" : @"success"};
// 2.执行通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// iOS8之后和之前应区别对待
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIUserNotificationTypeSound];
}
return YES;
}
/// 这个函数存在的意义在于:当用户在设置中关闭了通知时,程序启动时会调用此函数,我们可以获取用户的设置
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
/// 注册失败调用
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"远程通知注册失败:%@",error);
}
/// 用户同意后,会调用此程序,获取系统的deviceToken,应把deviceToken传给服务器保存,此函数会在程序每次启动时调用(前提是用户允许通知)
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"deviceToken = %@",deviceToken);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// userInfo
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 获取远程推送消息
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
// 有推送的消息,处理推送的消息
}
return YES;
}
/// iOS3之后才有,只有在程序处于后台时,用户点击了通知后才会被调用,应搭配didFinishLaunchingWithOptions使用
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// userInfo
}
{
"aps" : { // 必须有
"alert" : "string",
"body" : "string",
"badge" : number,
"sound" : "string"
},
"NotiId" : 20150821, // 自定义key值
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有