推送本地通知-接收本地通知 - (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);
}
本地通知.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];
    
}
未显示通知消息.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弹出提示后,再次点击"打开",进入应用,显示通知信息:

获取到本地通知信息.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • WWDC session - Notifications 学习总结,如有不妥之处,望请指正🙏🙏 pusher工具[...
    kurt_wang阅读 10,458评论 12 28
  • 推送通知注意:这里说的推送通知跟NSNotification有所区别NSNotification是抽象的,不可见的...
    醉叶惜秋阅读 5,443评论 0 3
  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 11,727评论 10 16
  • 本地通知:就是指不需要互联网就能发出的推送通知(不需要服务器去支持),使用的场景一般是定时提醒用户完成一些任务,例...
    JaXz阅读 5,382评论 0 3
  • 原理的重要性 选择目标的重要性 孔子与墨子的区别 实用性的有用的标准 动因理论 激励理论 1有挑战性2获得认可3获...
    魏sir阅读 3,012评论 0 0

友情链接更多精彩内容