iOS中提供了2種推送通知
- 本地推送通知(Local Notification)
- 遠程推送通知(Remote Notification)
推送通知的作用
- 可以讓不在前臺運行的app,告知用戶app內(nèi)部發(fā)生了什么事情
- 推送通知的呈現(xiàn)效果總結(jié)
- 用戶接收的推送通知,都會展示在“通知中心”
- 從屏幕頂部往下滑,就能調(diào)出“通知中心”
- 顯示橫幅還是UIAlertView,取決于用戶的設置
總結(jié)一下,推送通知有5種不同的呈現(xiàn)效果
- 在屏幕頂部顯示一塊橫幅(顯示具體內(nèi)容)
- 在屏幕中間彈出一個UIAlertView(顯示具體內(nèi)容)
- 在鎖屏界面顯示一塊橫幅(鎖屏狀態(tài)下,顯示具體內(nèi)容)
- 更新app圖標的數(shù)字(說明新內(nèi)容的數(shù)量)
- 播放音效(提醒作用)
** 推送通知的使用細節(jié)**
- 發(fā)出推送通知時,如果當前程序正運行在前臺,那么推送通知就不會被呈現(xiàn)出來
- 點擊推送通知后,默認會自動打開發(fā)出推送通知的app
- 不管app打開還是關(guān)閉,推送通知都能如期發(fā)出
本地推送
AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/* UIUserNotificationTypeNone = 0, 無類型(不給用戶發(fā)通知)
UIUserNotificationTypeBadge = 1 << 0, 是否可以改變應用圖標右上角的提示數(shù)字
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]) {
// 跳轉(zhuǎn) 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;
}
/**
* 點擊通知打開應用的時候會執(zhí)行該方法
* 應用在前臺的時候,收到通知也會執(zhí)行該方法
*
* @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.創(chuàng)建一個本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 1.1.設置通知發(fā)出的時間
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// 1.2.設置通知發(fā)出的內(nèi)容
localNote.alertBody = @"吃飯了嗎?";
// 1.3.是否彈出提示框
localNote.hasAction = YES;
// 1.4.設置提示框
localNote.alertAction = @"趕緊查看";
// 1.5.設置啟動的圖片
localNote.alertLaunchImage = @"1111";
// 1.6.設置啟動的音效
localNote.soundName = UILocalNotificationDefaultSoundName;
// 1.7.設置應用圖標提醒的數(shù)字
localNote.applicationIconBadgeNumber = 999;
// 1.8.如果想將通知的信息傳遞過去,必須使用userInfo屬性
localNote.userInfo = @{@"msg" : @"吃飯了嗎", @"date" : localNote.fireDate};
// 2.調(diào)度通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (IBAction)removeLocalNote {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// [UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)
}
@end
遠程推送
** AppDelegate.m**
#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]) {
// 頁面的跳轉(zhuǎn)
}
[application setApplicationIconBadgeNumber:0];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// 將用戶的用戶名和deviceToken發(fā)送給服務器,讓服務器進行保存?zhèn)浞菁纯? NSLog(@"%@", deviceToken);
}
/**
* 當接受到遠程通知的時候會調(diào)用該方法
*
* @param userInfo 遠程通知的信息
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// 在這里可以跳轉(zhuǎn)的其他頁面
NSLog(@"%@", userInfo);
}
/**
* 如果接受到遠程通知時,想要后臺執(zhí)行任務,則實現(xiàn)調(diào)用該方法
*
* @param userInfo
* @param completionHandler 后臺執(zhí)行完之后要告知系統(tǒng),是否更新成功
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"%@", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
@end