iOS推送本地通知 Push Notifications: local part

主要步骤:Steps:

  1. Request permission to notify the user.
  2. Create a notification object.
  3. Execute the notification.
  4. Prepare your app to receive the notification.
    • when it’s not running.
    • when it’s in any other lifecycle state
  5. Wire up the user interface

Code for step 1: Request Permission

在ViewController中添加如下方法的定义和实现

- (void) requestPermissionToNotify{
    //create types for wanted notifications
    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    //create settings for wanted types
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    //ask for permissions
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

}

Code for step 2&3


- (void) createANotification:(int)seconds_in_future{
    //create local notificaiton object
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];

    //set the fire time of this notification
    localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:seconds_in_future];

    //if you want the notification fire date to stick to one specific time zone
    localNotif.timeZone = nil;

    //set notification title
    localNotif.alertTitle = @"Alert Title ";

    //set notification body
    localNotif.alertBody = @"Alert Body";

    //provide an action to close notification
    localNotif.alertAction = @"confirm";

    //set the sound
    localNotif.soundName = UILocalNotificationDefaultSoundName;

    //set badge number, using 1018 for fun
    localNotif.applicationIconBadgeNumber = 1018;

    //schedule the notification into application
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

Code for step 4: Prepare your app to receive the notification

在AppDelegate.m中,重写两个方法,用于在不同情况下接收notification并响应

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //clear badge
    application.applicationIconBadgeNumber = 0;


    //request notification object to associate with a predefined key. If not launched by notification, it would be nil
    UILocalNotification *localNotif = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

    if(localNotif){
        UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"Received on launch" message:localNotif.alertBody preferredStyle:UIAlertControllerStyleAlert ];

        UIAlertAction *aa = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil];
        [ac addAction:aa];

        dispatch_async(dispatch_get_main_queue(), ^{
            [application .keyWindow.rootViewController presentViewController:ac animated:YES completion:nil];
        });
    }

    return YES;
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification{

    //clear badge
    application.applicationIconBadgeNumber = 0;


    //received a notification, display an alert controller
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"Receive while running" message:notification.alertBody preferredStyle:UIAlertControllerStyleAlert ];

    UIAlertAction *aa = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil];
    [ac addAction:aa];

    dispatch_async(dispatch_get_main_queue(), ^{
        [application .keyWindow.rootViewController presentViewController:ac animated:YES completion:nil];
    });
}

Code for step 5: Wire up the user interface

添加一个按钮,添加如下触发函数

- (IBAction)scheduleTapped:(id)sender {
    [self requestPermissionToNotify];
    [self createANotification:5]; //the number 5 here means 5 seconds
}

执行效果:

这里写图片描述

这里写图片描述

关闭后接收通知:
这里写图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值