推送通知的跳转

本文主要讲解:当手机接收到通知(远程推送通知或者是本地推送通知)的时候,点击通知的时候,进行相应的跳转.

示例代码:

  • AppDelegate.m 文件中
// App一旦开始运行, 就会执行该方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 通知配置 (具体的权限, 通知Category(不同的通知样式--包含Action事件))

    // 权限
    UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

    // 样式- 对应 -Category  (包含多个Action)
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
    category.identifier = @"CustomCategroyIdentifier";      // 通知会根据该ID来使用对应的Category样式

    // UIUserNotificationAction 表示 Category上的按钮
    UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
    action.title = @"按钮标题";
    action.identifier = @"CustomAction";
    action.activationMode = UIUserNotificationActivationModeBackground;   // 激活后的样式, 会进入App, 不会进入App, 但是都会触发代理

    // 为Category添加Action
    [category setActions:@[action] forContext:UIUserNotificationActionContextDefault];
    // 一个通知配置是可以注册多个Category
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:[NSSet setWithObjects:category, nil]];
    // 注册通知配置
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    /*================= 通知跳转 =================*/
    /**
     1. 获取到通知的相关内容
        launchOptions 包含了运行App时的一些相关参数
     */
    // UIApplicationLaunchOptionsLocalNotificationKey 指定, 是否是通过本地通知来启动的App, 所对应的Value是一个UILocalNotification对象
//    launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];  远程推送

    UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
    if (notification) {
        if (notification.userInfo[@"通知类型"]) {
            NSNumber *number = notification.userInfo[@"跳转索引"];

            // 此时 application.keyWindow 是nil,
            // 执行了 makeKeyAndVisible 的window 就是keywindow
            //            UITabBarController *tabVC = (UITabBarController *)application.keyWindow.rootViewController;

            // 代码实现跳转
            UITabBarController *tabVC = (UITabBarController *)self.window.rootViewController;
            [tabVC setSelectedIndex:number.intValue];
        }
    }  
    return YES;
}

#pragma mark - 点击通知后触发

// 点击了通知后进入App, 触发
/**
 * App处于后台时, 会弹出通知的Alert, 只有点击后进入App才会触发代理, 进行相关跳转
 * App处于前台时, 不会弹出通知的相关提示的, 但是会直接触发代理方法, 不应该进行跳转
 * App被杀掉时, 通知依旧会弹出来, 如果点击了通知, 会启动你的App. 代理方法不会生效, 
 此时要通过 didFinishLaunchingWithOptions : 完成相关操作
 */
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"点击了通知后触发: %@", notification);

    // 当用户点击了通知后, 在此处通过代理来实现界面跳转 (跳转的数据由通知提供)
    /**
     0. 判断当前的App状态, 处于前台是不跳转
     1. 判断该通知是否为需要跳转类型通知
     2. 获取跳转的参数
     3. 进行跳转
     */
    // App 当前所处于状态
    /**
     UIApplicationStateActive,          激活, 前台正在运行
     UIApplicationStateInactive,          没激活, 不是在后台, App不能响应事件处理
     UIApplicationStateBackground       在后台运行
     */
    if (application.applicationState != UIApplicationStateActive) {
        // notification 对象当中, 获取UserInfo
        if (notification.userInfo[@"通知类型"]) {
            NSNumber *number = notification.userInfo[@"跳转索引"];

            // 代码实现跳转
            UITabBarController *tabVC = (UITabBarController *)self.window.rootViewController;
            [tabVC setSelectedIndex:number.intValue];
        }
    }
}
#pragma mark - 点击了Category上的Action时触发
// iOS8
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
    NSLog(@"点击了Category上的Actoin触发: %@, %@", identifier, notification);

    // 当你处理完相关的操作后, 需要告诉系统
    completionHandler();
}
  • ViewController.m文件中
- (IBAction)addNotificationAction:(id)sender
{
    // UILocalNotification 表示本地通知
    UILocalNotification *notification = [[UILocalNotification alloc] init];

    // 通知内容
    notification.alertBody = @"科比一路走好!";
    // 只会显示在通知中心 / AppleWatch
    notification.alertTitle = @"骗你的!";
    // 手机的默认音效, 自定义音效 (保存在Bundle当中的音效文件名)
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 图标边缘数字
    notification.applicationIconBadgeNumber = 6;

    // 携带自定义数据 (用来跳转到TabbarController 用的数据)
    notification.userInfo = @{
                              @"通知类型" : @"News", // 通知类型名
                              @"跳转索引" : @(2),    // 通知携带的参数名
                              };

    // 应用已经注册好的Category
    // Category的identifier.
    notification.category = @"CustomCategroyIdentifier";

    // 基于时间
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    // 重复触发的时间隙间
    //    notification.repeatInterval = NSCalendarUnitDay;

    // 将通知添加到调度池等待被触发
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

本文章代码Demo链接:点我

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值