实用知识:点击通知进行界面转跳
来源:互联网 发布:java和javaweb 编辑:程序博客网 时间:2024/06/06 16:31
#import "AppDelegate.h"#import "MyNotificationIdentifierDefine.h"@interface AppDelegate ()@end@implementation AppDelegate// 当App通过点击通知启动的时候, 会执行该方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /*================= 通知的类别 =================*/ UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init]; // 通知可以通过该id来使用对应的类别 category.identifier = MyNotificationCategoryIdentifier; // Category 的配置 // category上的按钮 UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init]; action.title = @"进入前台模式"; // 不会跳转到App当中, 会触发代理 action.activationMode = UIUserNotificationActivationModeForeground; action.identifier = MyNotificationCategoryForcegroundActionIdentifier; UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; action2.title = @"进入后台模式"; // 不会跳转到App当中, 会触发代理 action2.activationMode = UIUserNotificationActivationModeBackground; action2.identifier = MyNotificationCategoryBackgroundActionIdentifier; [category setActions:@[action, action2] forContext:UIUserNotificationActionContextDefault]; /*================= 通知配置 =================*/ NSSet *set = [NSSet setWithObjects:category, nil]; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert categories:set]; // 注册 通知的配置, (本地通知/推送通知) [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; /*================= 点击通知跳转的第三种处理方式 =================*/ // 如果App是通过点击了本地通知启动的, 此时 notification 有值, 值就是触发的本地通知 // 如果App是正常启动的, 此时 notification 是空的 UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]; if (notification) { // 跳转到指定的页面 // keyWindow还没有更新, keyWindow是nil// UITabBarController *tabVC = (UITabBarController *)application.keyWindow.rootViewController; UITabBarController *tabVC = (UITabBarController *)self.window.rootViewController; tabVC.selectedIndex = [notification.userInfo[MyNotificationJumpToPageKey] unsignedIntegerValue]; } return YES;}#pragma mark - Category上的Action触发的代理方法// iOS8.0, 点击了通知上的Category里面的按钮(Action)时触发- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{ NSLog(@"HandleAction:%@, %@", identifier, notification); // 在处理完action相关操作后, 必须调用completionHandler completionHandler();}// iOS9.0, 点击了通知上的Category里面的按钮(Action)时触发//- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler//{// //}#pragma mark - 点击了通知时触发的代理方法// 点击了通知(横幅)是触发的代理方法 (App处于后台, 运行的)// 如果App正处于前台, 会直接触发代理方法 (App处于前台, 运行的)// 不会触发代理 (App挂了, 没有运行)- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ NSLog(@"点击了通知: %@", notification); // App当前的状态 /** UIApplicationStateActive, 激活, 正在前台 UIApplicationStateInactive, 没有激活, 处于最前, 但不响应事件 UIApplicationStateBackground 后台, 压缩到后台, 没有正在使用 */ if (application.applicationState == UIApplicationStateActive) { NSLog(@"正在前台, 不跳转"); return; } // 判断当前触发的通知是否需要进行页面跳转 if ([notification.userInfo[MyNotificaitonIdentifer] isEqualToString:@"JumpToPageNotification"]) { // 跳转到指定的页面 UITabBarController *tabVC = (UITabBarController *)application.keyWindow.rootViewController; tabVC.selectedIndex = [notification.userInfo[MyNotificationJumpToPageKey] unsignedIntegerValue]; }}@end
#ifndef MyNotificationIdentifierDefine_h#define MyNotificationIdentifierDefine_h#define MyNotificationCategoryIdentifier @"MyNotificationCategoryIdentifier"#define MyNotificationCategoryForcegroundActionIdentifier @"MyNotificationCategoryForceActionIdentifier"#define MyNotificationCategoryBackgroundActionIdentifier @"MyNotificationCategoryBackgroundActionIdentifier"#define MyNotificaitonIdentifer @"MyNotificaitonIdentifer" // 区分每一个通知的Key#define MyNotificationJumpToPageKey @"MyNotificationJumpToPageKey" // 跳转到指定页的Key#endif /* MyNotificationIdentifierDefine_h */
#import "ViewController.h"#import "MyNotificationIdentifierDefine.h"@interface ViewController ()@end@implementation ViewController- (IBAction)btnAction:(id)sender{ // 添加本地通知 UILocalNotification *notification = [[UILocalNotification alloc] init]; // 配置 notification.alertBody = @"通知内容"; notification.alertTitle = @"通知标题"; // 通知中心 / Watch // UILocalNotificationDefaultSoundName默认提示音 / 保存在Bundle中的音效文件的文件名 notification.soundName = UILocalNotificationDefaultSoundName; // 手动去修改回来, 否则一直存在 notification.applicationIconBadgeNumber = 6;// [UIApplication sharedApplication].applicationIconBadgeNumber = 0; notification.category = MyNotificationCategoryIdentifier; // 时间 / 位置 来触发通知 notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];// notification.repeatInterval // 使用userInfo来携带数据 notification.userInfo = @{ MyNotificaitonIdentifer : @"JumpToPageNotification", MyNotificationJumpToPageKey : @(2) }; // 定位授权, whenInUse与always// notification.region// notification.regionTriggersOnce // 添加到通知调度池中 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; // 调度池当中所有的通知// notification.userInfo = @{};// [[UIApplication sharedApplication] scheduledLocalNotifications];// [[UIApplication sharedApplication] cancelLocalNotification:nil];}@end
0 0
- 实用知识:点击通知进行界面转跳
- 实用知识:关于通知里面的 category 使用方法
- 实用知识:基于监听位置的本地通知 使用方法
- (浅谈)Jpush推送通知点击跳转到具体界面
- 用通知进行两个界面之间的回调传值
- IOS实用图片无限轮播 点击图片进行跳转
- android 下载应用 通知栏显示进度 下完之后点击安装 (很实用)
- 实用网站/实用知识
- MVC界面点击下载进行下载JSON文件
- Android 点击通知栏消息 跳转到指定的Fragment界面
- Android 点击通知栏消息 跳转到指定的Fragment界面
- viewpager图片广告条点击转跳fragment-----【轮播图点击转跳详情界面】
- 通知栏,点击事件!
- 点击通知启动App
- 自定义通知界面
- IOS 点击通知消息 跳至对应页面
- Android通知栏点击通知消失
- Notification通知,点击通知后跳转
- Android Fragment 真正的完全解析(上)
- BZOJ 1024 生日快乐(DFS)
- maven配置
- 《OS之缓冲技术》
- Python 多线程 生产者消费者模型
- 实用知识:点击通知进行界面转跳
- Android Fragment 真正的完全解析(下)
- mathtype批量修改
- HDU1671 ①string char一维/二维数组 的比较 ②字典树
- 编程知识如何分类呢?
- HDU 5444 Elven Postman
- 树链剖分
- 如何使用XMLHTTPRequest对象实现无刷新效果
- matlab中字符串支持latex