美文网首页iOS开发经验APNsiOS Developer
iOS 推送通知-杀死App点击通知栏跳转到指定页面

iOS 推送通知-杀死App点击通知栏跳转到指定页面

作者: Hellolad | 来源:发表于2016-09-27 23:25 被阅读4710次

杀死app点击通知栏跳转到指定页面(以本地通知为例)

第一步:两个控制器

1.ViewController
2.HelloViewController
我们杀死App或者在后台的时候点击通知栏跳转到HelloViewController里

AppDelegate.m
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.window.rootViewController = [ViewController new];
    
// 如果 launchOptions 不为空
if (launchOptions) {
    // 获取推送通知定义的userinfo
    UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
    NSString *controller = notification.userInfo[@"controller"];
    // 这里就是告诉程序我要跳转到哪里
    if ([controller isEqualToString:NSStringFromClass([HelloViewController class])]) {
        HelloViewController *hello = [HelloViewController new];
        hello.userInfo = launchOptions;
        [self.window.rootViewController presentViewController:hello animated:YES completion:nil];
    }
}

第二步:然后在ViewController.m里加入通知(具体在哪这个随你的项目去设置)
ViewController.m
// 注册通知 iOS8以后用这个 (现在已经有iOS10的新的推送方法了)
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    
// 删除所有的通知 这句话需不需要看你的需求了。
[[UIApplication sharedApplication] cancelAllLocalNotifications];
    
UILocalNotification *noti = [[UILocalNotification alloc] init];
noti.repeatInterval = NSCalendarUnitMinute; // 每分钟推送一次
noti.fireDate = [NSDate date];  // 当前时间
noti.alertBody = @"推送消息";
noti.alertTitle = @"Test";
noti.soundName = UILocalNotificationDefaultSoundName;
noti.userInfo = @{@"controller": @"HelloViewController"};
[[UIApplication sharedApplication] scheduleLocalNotification:noti]; // 加入推送
最后一步:在HelloViewController里我们做一些事情把页面改成红色告诉我们已经成功跳转到这里了。
HelloViewController.m
self.view.backgroundColor = [UIColor redColor];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, 200)];
UILocalNotification *notification = self.userInfo[UIApplicationLaunchOptionsLocalNotificationKey];
textView.text = [NSString stringWithFormat:@"%@", notification.userInfo[@"controller"]];
[self.view addSubview:textView];
OK 运行 回到杀死App 点击推送消息 跳转完成
本地推送.png

<第一次写简书,是一些比较简单的东西,工作中遇到了就记录下来了,请尽力吐槽。>

相关文章

网友评论

  • MichalWilson:你这代码我就复制黏贴试了一下,通知就停不下来了,代码都删完了,还一直不停的发通知,无语了
  • seanward:“后台或者杀死app点击通知栏”,是走的两个方法吧?在后台的时候走didReceiveRemoteNotification方法,杀死app的时候才是走didFinishLaunchingWithOptions这个方法,是吗
    Hellolad:@seanward 我这个是针对杀死App的情况,sorry.
  • Amok校长:我能实现收到通知后的跳转操作.但是没太明白,怎么区分是:在后台点击通知栏进入APP后跳转的 ,还是在运行APP的时候有通知接收到后跳转的
    Hellolad:@kirs 在后台和在玩其他App时候收到通知点击之后跳转都是从后台跳转吧,还有就是你在本App活跃状态下是收不到通知栏通知的
  • 施主小欣:请问楼主有详细的demo提供参考嘛?
    施主小欣:@RSFork 好的 谢谢!
    Hellolad:@施主小欣 这个就是全部代码了
  • c066fe49abc4:你好,请问你有没有遇到过点击推送消息跳转到相应的页面后,该页面的点击事件不响应,用的是present的方法 :smile:
  • 蛋上有皱纹:继续努力,更上一层楼

本文标题:iOS 推送通知-杀死App点击通知栏跳转到指定页面

本文链接:https://www.haomeiwen.com/subject/arkhyttx.html