iOS 本地推播通知和接收通知及其跳轉控制總結,ios跳轉

來源:互聯網
上載者:User

iOS 本地推播通知和接收通知及其跳轉控制總結,ios跳轉

iOS 本地推播通知和接收通知及其跳轉控制總結。

1. 本地通知 iOS8.01.1. 本地通知發送
***iOS8.0之後才能用    //本地通知    UILocalNotification *locationNo = [[UILocalNotification alloc] init];    //觸發時間    locationNo.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];    //通知內容    locationNo.alertBody = @"這是本地通知";    //iOS8.2之後可見,一般不用    if (@available(iOS 8.2,*)) {        locationNo.alertTitle = @"哈哈,本地推送";    }    //鎖屏(黑屏狀態下,"滑動來"後面的字)--iOS10.0之後,沒有滑動解鎖功能了,這個屬性也就不存在了    locationNo.alertAction = @"查看這個本地推送";    locationNo.hasAction = YES;    //通知過來時的聲音    locationNo.soundName = UILocalNotificationDefaultSoundName;    //應用表徵圖提示--預設是0,沒有改變,設定為負數,通知到達時表徵圖提示就會消失    locationNo.applicationIconBadgeNumber = -1;    //直接設定應用表徵圖提示為0,代表隱藏,和上面效果一致    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;    //MARK: 額外資訊--對於使用者不重要,對於開發人員很重要,用於接收通知時處理相應邏輯    locationNo.userInfo = @{@"name":@"周玉",@"job":@"iOS開發工程師"};    //加入到調度池---有可能有多個通知---取決於觸發時間    [[UIApplication sharedApplication] scheduleLocalNotification:locationNo];
1.2. 本地通知接收
//#import "AppDelegate.h" 註冊通知- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    /*     UIUserNotificationTypeNone    = 0,           UIUserNotificationTypeBadge   徽章標記     UIUserNotificationTypeSound   聲音     UIUserNotificationTypeAlert   彈出效果     */    //註冊通知    //本地通知設定    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];    //當程式被殺死的情況下,接收到通知並執行事情--    UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];    if (notification) {        NSLog(@"localNo = %@",notification.userInfo);//NSLog不會再列印        //用測測試是否接收到通知        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];        label.backgroundColor = [UIColor redColor];        [self.window.rootViewController.view addSubview:label];    return YES;}
// 程式沒有被殺死時,接收到本地通知時調用--點擊通知時會執行這個方法,如果程式被殺死時,這個方法不再走,在application: didFinishLaunchingWithOptions:方法中擷取- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{    NSLog(@"notification.userInfo = %@",notification.userInfo);

//鎖屏(黑屏狀態下,”滑動來”後面的字)–iOS10.0之後,沒有滑動解鎖功能了,這個屬性也就不存在了
locationNo.alertAction = @”查看這個本地推送”;
locationNo.hasAction = YES;

2. 本地通知邏輯處理 iOS8.02.1. 發送推播通知
//  發送通知//  ViewController.m//  本地推送////  Created by zhouyu on 2017/12/27.//  Copyright ? 2017年 zhouyu. All rights reserved.//#import "ViewController.h"#import "RedController.h"#import "BlueController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.title = @"本地推送";    [self setUpUI];}- (void)setUpUI{    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 150, 60)];    [btn1 setTitle:@"跳轉紅色控制器" forState:UIControlStateNormal];    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];    [btn1 addTarget:self action:@selector(red) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn1];    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 150, 60)];    [btn2 setTitle:@"跳轉藍色控制器" forState:UIControlStateNormal];    [btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];    [btn2 addTarget:self action:@selector(blue) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn2];}- (void)red{    [self pushLocationNotificationWithAlertBody:@"跳轉紅色控制器" userInfo:@{@"key":@"red"}];}- (void)blue{    [self pushLocationNotificationWithAlertBody:@"跳轉藍色控制器" userInfo:@{@"key":@"blue"}];}- (void)pushLocationNotificationWithAlertBody:(NSString *)body userInfo:(NSDictionary *)userInfo{    //本地通知    UILocalNotification *locationNo = [[UILocalNotification alloc] init];    //觸發時間    locationNo.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];    //通知內容    locationNo.alertBody = body;    //通知過來時的聲音    locationNo.soundName = UILocalNotificationDefaultSoundName;    //應用表徵圖提示--預設是0,沒有改變,設定為負數,通知到達時表徵圖提示就會消失    locationNo.applicationIconBadgeNumber = -1;    //直接設定應用表徵圖提示為0,代表隱藏,和上面效果一致    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;    //MARK: 額外資訊--對於使用者不重要,對於開發人員很重要    locationNo.userInfo = userInfo;    //加入到調度池---有可能有多個通知---取決於觸發時間    [[UIApplication sharedApplication] scheduleLocalNotification:locationNo];}@end
2.2. AppDelegate接收推播通知進行邏輯處理
//  接收通知並進行邏輯處理//  AppDelegate.m//  本地推送////  Created by zhouyu on 2017/12/27.//  Copyright ? 2017年 zhouyu. All rights reserved.//#import "AppDelegate.h"#import "ViewController.h"#import "RedController.h"#import "BlueController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];    [self.window makeKeyAndVisible];    self.window.backgroundColor = [UIColor whiteColor];    /*     UIUserNotificationTypeNone    = 0,           UIUserNotificationTypeBadge   徽章標記     UIUserNotificationTypeSound   聲音     UIUserNotificationTypeAlert   彈出效果     */    //註冊通知    //本地通知設定    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];    //當程式被殺死的情況下,如何接收到通知並執行事情--ios10.0之後廢棄,需要用10.0之前版本測試    UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];    if (notification) {        NSLog(@"localNo = %@",notification.userInfo);//NSLog不會再列印        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];        label.backgroundColor = [UIColor redColor];        [self.window.rootViewController.view addSubview:label];        [self jumpToControllerWithLocationNotification:notification];    }    return YES;}// 程式沒有被殺死時,接收到本地通知時調用--點擊通知時會執行這個方法,如果程式被殺死時,這個方法不再走,在application: didFinishLaunchingWithOptions:方法中擷取- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{    NSLog(@"notification.userInfo = %@",notification.userInfo);    [self jumpToControllerWithLocationNotification:notification];}- (void)jumpToControllerWithLocationNotification:(UILocalNotification *)localNo{    //如果APP在前台,就不用走通知的方法了    if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {        return;    }    //擷取userInfo    NSDictionary *userInfo = localNo.userInfo;    UINavigationController *nav = (UINavigationController *)self.window.rootViewController;    //判斷跳轉    if ([userInfo[@"key"] isEqualToString:@"red"]) {        [nav pushViewController:[[RedController alloc] init] animated:YES];    } else if([userInfo[@"key"] isEqualToString:@"blue"]) {        [nav pushViewController:[[BlueController alloc] init] animated:YES];    }}@end

//如果APP在前台,就不用走通知的方法了
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
return;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.