iOS 推送通知
2015-11-12 本文已影响2961人
iOS_成才录
- 注意:通知是一个应用程序级别的操作UIApplication
推送通知 与 NSNotification 区别
- NSNotification是抽象的,不可见的
- 推送通知是可见的(能用肉眼看到)
iOS中提供了2种推送通知
-
本地
推送通知(Local Notification) -
远程
推送通知(Remote Notification)
推送通知的作用
- 可以让
不在前台运行的ap
p,告知用户app内部发生了什么事情
推送通知的呈现效果总结
- 用户接收的推送通知,都会展示在“通知中心”
- 从屏幕顶部往下滑,就能调出“通知中心”
- 显示横幅还是UIAlertView,取决于用户的设置
总结一下,推送通知有5种不同的呈现效果
- 在屏幕顶部显示一块横幅(
显示具体内容
) - 在屏幕中间弹出一个UIAlertView(
显示具体内容
) - 在锁屏界面显示一块横幅(锁屏状态下,
显示具体内容
) - 更新app图标的数字(
说明新内容的数量
) - 播放音效(
提醒作用
)
推送通知的使用细节
-
发出推送通知时,如果
当前程序正运行在前台
,那么推送通知就不会被呈现出来 -
点击推送通知后,默认会自动打开发出推送通知的app
-
不管app打开还是关闭,推送通知都能如期发出
本地推送
- AppDelegate.h文件
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/*
UIUserNotificationTypeNone = 0, 无类型(不给用户发通知)
UIUserNotificationTypeBadge = 1 << 0, 是否可以改变应用图标右上角的提示数字
UIUserNotificationTypeSound = 1 << 1, 该通知是否会有声音
UIUserNotificationTypeAlert = 1 << 2, 是否有弹出提示
*/
if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
// 跳转
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 300, 300, 300);
label.backgroundColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@"%@", launchOptions];
label.font = [UIFont systemFontOfSize:14];
label.numberOfLines = 0;
[self.window.rootViewController.view addSubview:label];
}
return YES;
}
/**
* 点击通知打开应用的时候会执行该方法
* 应用在前台的时候,收到通知也会执行该方法
*
* @param notification 通知
*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 0, 300, 300);
label.backgroundColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@"%@", notification];
label.font = [UIFont systemFontOfSize:14];
label.numberOfLines = 0;
[self.window.rootViewController.view addSubview:label];
// if (application.applicationState == UIApplicationStateBackground) {
//
// }
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return YES;
}
@end
- 推送
#import "ViewController.h"
@interface ViewController ()
/**
* 点击按钮后添加本地通知
*/
- (IBAction)addLocalNote;
/**
* 移除通知(不常用)
*/
- (IBAction)removeLocalNote;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
/**
* 点击按钮后添加本地通知
*/
- (IBAction)addLocalNote {
/*
@property(nonatomic,copy) NSDate *fireDate;
@property(nonatomic,copy) NSTimeZone *timeZone;
@property(nonatomic) NSCalendarUnit repeatInterval;
@property(nonatomic,copy) NSCalendar *repeatCalendar;
@property(nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0);
@property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0);
@property(nonatomic,copy) NSString *alertBody;
@property(nonatomic) BOOL hasAction;
@property(nonatomic,copy) NSString *alertAction;
@property(nonatomic,copy) NSString *alertLaunchImage;
@property(nonatomic,copy) NSString *soundName; UILocalNotificationDefaultSoundName
@property(nonatomic) NSInteger applicationIconBadgeNumber;
@property(nonatomic,copy) NSDictionary *userInfo;
*/
// 1.创建一个本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 1.1.设置通知发出的时间
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// 1.2.设置通知发出的内容
localNote.alertBody = @"吃饭了吗?";
// 1.3.是否弹出提示框
localNote.hasAction = YES;
// 1.4.设置提示框
localNote.alertAction = @"赶紧查看";
// 1.5.设置启动的图片
localNote.alertLaunchImage = @"1111";
// 1.6.设置启动的音效
localNote.soundName = UILocalNotificationDefaultSoundName;
// 1.7.设置应用图标提醒的数字
localNote.applicationIconBadgeNumber = 999;
// 1.8.如果想将通知的信息传递过去,必须使用userInfo属性
localNote.userInfo = @{@"msg" : @"吃饭了吗", @"date" : localNote.fireDate};
// 2.调度通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (IBAction)removeLocalNote {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// [UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)
}
@end
远程推送
- 1.appDelegate.h文件
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
// 1.向用户请求可以给用户推送消息
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[application registerUserNotificationSettings:settings];
// 2.注册远程通知(拿到用户的DeviceToken)
[application registerForRemoteNotifications];
} else {
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
// 页面的跳转
}
[application setApplicationIconBadgeNumber:0];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// 5e8cf393 9e950137 86ac8375 12185078 19eb3ebd 936777e1 f061caec a48cb236
// 将用户的用户名和deviceToken发送给服务器,让服务器进行保存备份即可
NSLog(@"%@", deviceToken);
}
/**
* 当接受到远程通知的时候会调用该方法
*
* @param userInfo 远程通知的信息
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// 在这里可以跳转的其他页面
NSLog(@"%@", userInfo);
}
/**
* 如果接受到远程通知时,想要后台执行任务,则实现调用该方法
*
* @param userInfo
* @param completionHandler 后台执行完之后要告知系统,是否更新成功
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"%@", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
@end