与时俱进的IT

UI(四十四)本地推送

2018-07-30  本文已影响0人  社会主义顶梁鹿

*推送:

 1、远程推送(Remote Notification)

 2、本地推送(local Notification)

 作用:可以让APP不在前台,告知用户APP内部发生的事

 效果:

 1、没有效果

 2、在屏幕顶部显示横幅(显示具体内容)

 3、在屏幕中间弹出弹出框(AlertController)(显示具体内容)

 4、在锁屏时显示一块横幅

 5、可以更改APP图标上显示的提醒数字

- 播放音效

- 推送通知的使用细节

 注意:发送推送通知的时候,如果APP在前台运行,那么推送的通知不会被呈现出来

 在发送通知之后,无论APP是打开还是关闭,推送通知都能如期发送,但是用户不一定能如期去接收

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

 //判断系统版本

 if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {

 /*

         UIUserNotificationTypeNone  :没有样式

         UIUserNotificationTypeBadge :是否可以改变APP图标右上角的提示数字

         UIUserNotificationTypeSound:改通知是否有声音

         UIUserNotificationTypeAlert:是否弹出提示

         */

 //可以同时设置三种样式 或|

 UIUserNotificationSettings*settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];

 //注册通知对象

[application registerUserNotificationSettings:settings];

    }

 //用作点击推送的时候跳转

 //判断是否点击

 if(launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {

 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 300, 300, 200)];

        label.text =[NSString stringWithFormat:@"%@",launchOptions];

        label.font =[UIFont systemFontOfSize:14];

        label.numberOfLines =0;

        [self.window.rootViewController.view addSubview:label];

    }

 return YES;

}

//点击通知打开应用的时候会执行该方法 在前台收到通知的时候也会调用该方法

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

 //用作点击推送的时候跳转

 //判断是否点击

 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 300, 300, 200)];

        label.text =[NSString stringWithFormat:@"%@",notification];

        label.font =[UIFont systemFontOfSize:14];

        label.numberOfLines =0;

        [self.window.rootViewController.view addSubview:label];

}

//1、创建一个本地通知对象

 UILocalNotification *localNot = [[UILocalNotification alloc]init];

 //2、设置具体属性

 //2.1、设置通知发送的时间

    localNot.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

 //2.2、设置发送的内容

    localNot.alertBody = @"我是一条通知";

 //2.3、设置是否显示提示框

    localNot.hasAction = YES;

 //2.4、设置提示框

    localNot.alertAction = @"赶紧去看看";

 //2.5、设置APP图标提醒数字

    localNot.applicationIconBadgeNumber = 8;

 //2.6、设置应用的提示声音

    localNot.soundName = UILocalNotificationDefaultSoundName;

 //3、去调度通知

    [[UIApplication sharedApplication]scheduleLocalNotification:localNot];

    [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];

上一篇 下一篇

猜你喜欢

热点阅读