推送本地通知-接收本地通知 - (Obj-C)

创建本地通知,并给通知设置一个传递的信息

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

// 创建本地通知
- (IBAction)StartLocalNoteButtonClick:(id)sender {
    
    // 创建本地通知
    UILocalNotification *localNotification = [[UILocalNotification alloc]init];
    
    // 设置属性
    // 1.触发事件
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    // 2.设置通知的显示内容
    localNotification.alertBody = @"收到一条新消息";
    
    // 设置传递的信息(自己设置,任意的Key,获取通知的时候需要使用此Key)
    localNotification.userInfo = @{@"content":@"你妈喊你回家吃饭"};
    
    // 预定通知(UIApplication 作为系统和应用的桥梁,一般负责应用和系统相关的工作)
    // iOS 8.0以后,在预定和展示通知之前必须要注册通知,请求授权
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 注册通知,请求授权
    /*
         UIUserNotificationTypeNone  无任何效果
         UIUserNotificationTypeBadge 通知含有角标
         UIUserNotificationTypeSound 带声音
         UIUserNotificationTypeAlert 提示文字
     */
    UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    
}


@end

在AppDelegate代理方法中接收到通知:

  • 应用在后台时,点击通知进入应用后调用,应用在前台时,直接接收到通知但是没有视图变化
/**
 *  当已经接收到本地通知后调用 
 *
 *  @param application  应用对象
 *  @param notification 接收到的本地通知
 */
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    
    NSString *contentString = notification.userInfo[@"content"];
    NSLog(@"%@",contentString);
}
推送本地通知-接收本地通知 - (Obj-C)_第1张图片
本地通知.png

点击创建本地通知后,切换至后台,待5s后会收到本地通知,只有点击"打开"按钮进入程序的时候,才会执行didReceiveLocalNotification方法,打印结果:

本地通知[34499:293172] 你妈喊你回家吃饭
  • 如果将应用杀死了,也是可以获取通知的
    但并不能通过didReceiveLocalNotification方法获取通知信息了
    这里因为将应用杀死无法通过控制台打印判断,为了验证,接下来在视图界面上添加一个Label,用来显示获取的通知信息:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    
    NSString *contentString = notification.userInfo[@"content"];
    NSLog(@"%@",contentString);
    [self showLocalNote:notification];
}

- (void)showLocalNote:(UILocalNotification *)localNote{
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 200, 100)];
    label.textColor = [UIColor blueColor];
    label.text = localNote.userInfo[@"content"];
    [self.window.rootViewController.view addSubview:label];
    
}
推送本地通知-接收本地通知 - (Obj-C)_第2张图片
未显示通知消息.png

通过结果判断,虽然接收到了通知,但是点击"打开"进入应用后并未收到通知的信息,原因是杀死应用后,点击通知的"打开"按钮,相当于重新打开了一次应用,就不会再调用didReceiveLocalNotification方法了

如果应用是退出状态,接收到通知需要从didFinishLaunchingWithOptions方法中获取通知信息:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    /*
         NSString *const UIApplicationLaunchOptionsURLKey;
         NSString *const UIApplicationLaunchOptionsSourceApplicationKey;
         NSString *const UIApplicationLaunchOptionsRemoteNotificationKey;
         NSString *const UIApplicationLaunchOptionsLocalNotificationKey;
         NSString *const UIApplicationLaunchOptionsAnnotationKey;
         NSString *const UIApplicationLaunchOptionsLocationKey;
         NSString *const UIApplicationLaunchOptionsNewsstandDownloadsKey;
         NSString *const UIApplicationLaunchOptionsBluetoothCentralsKey;
         NSString *const UIApplicationLaunchOptionsBluetoothPeripheralsKey;
         NSString *const UIApplicationLaunchOptionsShortcutItemKey;
         NSString *const UIApplicationLaunchOptionsUserActivityDictionaryKey;
         NSString *const UIApplicationLaunchOptionsUserActivityTypeKey;
     */
    UILocalNotification *localNote = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
    [self showLocalNote:localNote];
    
    return YES;
}

点击创建本地通知后,结束应用,等待5s弹出提示后,再次点击"打开",进入应用,显示通知信息:

推送本地通知-接收本地通知 - (Obj-C)_第3张图片
获取到本地通知信息.png

你可能感兴趣的:(推送本地通知-接收本地通知 - (Obj-C))