-
本地推送
- "AppDelegate.m"
/*
UIUserNotificationTypeBadge 圖標(biāo)標(biāo)記 001
UIUserNotificationTypeSound 聲音 010
UIUserNotificationTypeAlert 彈框 100
111
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 注冊(cè)推送通知
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 當(dāng)程序被殺死情況下接收本地推送通知
UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 220, 200)];
label.backgroundColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@"%@",localNotification.userInfo];
label.numberOfLines = 0;
[self.window.rootViewController.view addSubview:label];
}
return YES;
}
/// 獲取本地推送通知攜帶的信息
/// 在前臺(tái)也會(huì)調(diào)用以下方法
/// 程序被殺死不會(huì)調(diào)用以下方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"%@",notification.userInfo);
// 控制臺(tái)調(diào)試(前提條件 :程序需要和xcode連接)
// UI調(diào)試
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 220, 200)];
label.backgroundColor = [UIColor redColor];
[self.window.rootViewController.view addSubview:label];
}
- ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 設(shè)置0去隱藏圖標(biāo)標(biāo)記
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 創(chuàng)建本地推送通知
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// 通知觸發(fā)時(shí)間
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// 推送通知的內(nèi)容
localNotification.alertBody = @"xxx: xxxxx.";
// 推送的聲音
localNotification.soundName = UILocalNotificationDefaultSoundName;
// 應(yīng)用圖標(biāo)標(biāo)記 0 代表不改變?cè)瓉頂?shù)字 負(fù)值可以銷毀圖標(biāo)標(biāo)記
localNotification.applicationIconBadgeNumber = 10;
// 修改通知中心的名稱
// localNotification.alertTitle = @"嘻嘻嘻";
// // 修改鎖屏?xí)r'滑動(dòng)來' 后面的文字
// localNotification.alertAction = @"哈哈哈";
// 通知攜帶的具體信息 讓程序員使用
localNotification.userInfo = @{@"content":@"wokaiwanxiaode",@"name":@"yadan"};
// 將通知加入調(diào)度池
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
-
遠(yuǎn)程推送
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 注冊(cè)推送通知
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 向蘋果請(qǐng)求deviceToken 將UDID + BundleID 發(fā)送給蘋果
[[UIApplication sharedApplication] registerForRemoteNotifications];
// 程序被殺死情況下獲取遠(yuǎn)程推送信息
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]){
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
label.text = [NSString stringWithFormat:@"%@",launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
label.numberOfLines = 0;
[self.window.rootViewController.view addSubview:label];
}
return YES;
}
// 蘋果返回deviceToken時(shí)調(diào)用
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// 05859cfd 54157d56 a7ea2046 2087d0e9 f96ae9ca fbd73911 dc097c08 9a936879
NSLog(@"%@",deviceToken);
// 發(fā)送給你們公司的服務(wù)器
}
/// 接收遠(yuǎn)程推送信息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%@",userInfo);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
label.text = [NSString stringWithFormat:@"%@",userInfo];
label.numberOfLines = 0;
[self.window.rootViewController.view addSubview:label];
}
-
遠(yuǎn)程推送, 示意圖
遠(yuǎn)程推送deviceToken.png