实用知识:点击通知进行界面转跳

来源:互联网 发布: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