实用知识:本地推送的方法使用

来源:互联网 发布:矩阵式柔性充电堆技术 编辑:程序博客网 时间:2024/06/06 08:38
#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // 使用通知, 需要获取授权    // 从 UIApplication 类当中找到授权    /**     UIUserNotificationTypeNone    = 0,      没有     UIUserNotificationTypeBadge   = 1 << 0, 图标边缘数字     UIUserNotificationTypeSound   = 1 << 1, 通知提示声音     UIUserNotificationTypeAlert   = 1 << 2, 弹窗 (横幅/AlertView)     */    UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;    // UIUserNotificationSettings 表示App可以使用的通知的展示类型(声音, 边缘数字, 弹窗)    // category 是用来配置 按钮的    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];    // 注册用户通知的配置(通知使用的类型), (会弹出授权窗口来请求)    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];    return YES;}- (void)applicationWillEnterForeground:(UIApplication *)application{    NSLog(@"来了");    // 需要手动的把 applicationIconBadgeNumber 设置为0 , 才能让边缘数字消失    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;}@end
#import "ViewController.h"#define MyUserLocalNotificationIdentifer @"MyUserLocalNotificationIdentifer"@interface ViewController ()// 移除指定的通知时, 使用属性来记录是不正确的//@property (strong, nonatomic) UILocalNotification *notification;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];}- (IBAction)btnAction:(id)sender{    // 使用通知, 用户授权    /*================= 召召, 补*, 定时提醒, 本地通知 =================*/    // UILocalNotification 表示本地通知, 两种触发类型: 时间 / 区域    // 1. 实例化    UILocalNotification *notification = [[UILocalNotification alloc] init];    // 2. 给通知配置内容    // 2.1 通知的触发时间, 4秒后触发    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:4];    // 2.2 通知的内容    notification.alertBody = @"天气好晴朗, 该出去走走了";    // 2.3 通知的标题, 显示在通知中心/Watch的通知, 横幅上的名字是App名字    notification.alertTitle = @"要吃糖";    // 2.4 通知的提示音, UILocalNotificationDefaultSoundName默认提示音, 模拟器默认没声音//    notification.soundName = UILocalNotificationDefaultSoundName;    // 使用在bundle当中的音效文件的名字,  .mp3, .aac, .wav    notification.soundName = @"buyao.wav";    // 2.5 图标边缘数字, 需要手动修改回来, 否则一直为6    notification.applicationIconBadgeNumber = 6;    // 2.6 通知的重复间隙(重复执行) 最小是单位是分钟, 配置了会重复执行    // 默认只执行一次    // 重复执行的, 在执行完一次后并不会从通知调试池中调出    notification.repeatInterval = NSCalendarUnitMinute;    // 2.7 通过userInfo来携带我们需要的数据    notification.userInfo = @{                              MyUserLocalNotificationIdentifer : @"ZhaoZhao"                              };    // 3. 等待被触发 (UIApplication)    // 将通知添加到 通知调度池, 等待被调度(scheduleLocal)    [[UIApplication sharedApplication] scheduleLocalNotification:notification];}- (IBAction)seeBtnAction:(id)sender{    // 获取当前通知调试池当中的所有通知    NSArray <UILocalNotification *> *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];    NSLog(@"%@", notifications);}// 移除指定的通知- (IBAction)removeBtnAction:(id)sender{    // 移除指定通知时, 不能使用属性来记录    // 1. 去通知调试池当中查找, 查找到要移除的通知    NSArray <UILocalNotification *> *notications = [[UIApplication sharedApplication] scheduledLocalNotifications];    for (UILocalNotification *notification in notications) {        // 判断条件        [notification.userInfo[MyUserLocalNotificationIdentifer] isEqualToString:@"ZhaoZhao"];        // 2. 移除        [[UIApplication sharedApplication] cancelLocalNotification:notification];    }}- (IBAction)removeAllBtnAction:(id)sender{    // 移除所有的本地通知    [[UIApplication sharedApplication] cancelAllLocalNotifications];}@end
0 0