IOS中程序如何進行推送消息(本地推送,遠程推送)
---------------轉載注明出處----------
由于本地推送消息和遠程推送消息有部分機制不一樣,所有此demo寫本地推送,關于遠程推送我會在后面的博客上補上.
[1]-------------什么是推送消息? 我就以一張圖解釋------------
[2]-----------IOS程序中如何進行本地推送?-----------
2.1,先征求用戶同意
1 /** 2 * IOS8以后,推送通知需要征求用戶同意 3 */ 4 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]; 5 [application registerUserNotificationSettings:settings]; 6 NSLog(@"當前APP沒有死掉----%@",launchOptions);
2.2創建本地通知對象( UILocalNotification 這個類來管理,設置什么時候通知,通知的消息內容等)
UILocalNotification *localN = [UILocalNotification new];
設置處理通知的一些屬性
fireDate 時間
alertTitle 標題
alertBody 通知內容
soundName 聲音 ,
applicationIconBadgeNumber 圖標數字
alertAction 鎖屏狀態下的通知消息
其他參數:
timeZone 時區
repeatInterval 重復間隔...
alertLaunchImage 點擊通知的啟動圖片
2.3安排通知
[[UIApplication sharedApplication]scheduleLocalNotification:(UILocalNotification*)];
2.4安排通知后,就會在手機上收到了,此時需要處理點擊接受到的通知后的處理
/** 接收本地通知 程序在前臺 程序在后臺 鎖屏 */
- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"%@",notification);
} 2.5 可以根據需求,取消通知.
--------------放xcode截圖----------最后放源碼------注意是折疊源碼-------------
-------再感受一下通知-----當程序被用戶向上劃掉了也能接收通知-------
---------源碼,折疊的----------
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2 /**
3 * IOS8以后,推送通知需要征求用戶同意
4 */
5 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
6 [application registerUserNotificationSettings:settings];
7 NSLog(@"當前APP沒有死掉----%@",launchOptions);
8
9 //-------------------進程死掉分割線--------------
10
11
12 /**
13 * APP死掉后,會進入該方法
14 通過launchOptions的 key UIApplicationLaunchOptionsLocalNotificationKey
15 這個key 取得 本地通知對象 有通知對象 就代表是點通知進來的
16 */
17 if (launchOptions) {
18 /**
19 APP死掉情況下,點擊通知,我在rootViewController添加一個lable
20 */
21 UILabel *label = [[UILabel alloc]init];
22 label.backgroundColor = [UIColor redColor];
23 label.frame = CGRectMake(50, 100, 300, 200);
24 label.numberOfLines = 0;
25 label.text = [launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description];
26 label.font = [UIFont systemFontOfSize:11];
27 [self.window.rootViewController.view addSubview:label];
28 //程序死掉,點擊通知進入設置角標為0
29 application.applicationIconBadgeNumber = 0;
30 }
31
32 return YES;
33 }
34 /** APP未死,點擊接收到的本地通知后,會執行該方法*/
35 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
36 {
37 NSLog(@"點擊了接收到了本地通知");
38 /**
39 * 把程序角標設置為0,因為你是點擊通知進來的
40 */
41 application.applicationIconBadgeNumber = 0;
42 }
AppDelegate.m 1 #import "ViewController.h"
2
3 @interface ViewController ()
4 //聲明一個通知屬性
5 @property (nonatomic,strong)UILocalNotification *localNoti;
6 @end
7
8 @implementation ViewController
9
10 - (void)viewDidLoad {
11 [super viewDidLoad];
12
13 }
14
15 //注冊本地通知
16 - (IBAction)registerBtn:(id)sender {
17
18 self.localNoti = [UILocalNotification new];
19 self.localNoti.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];//5秒后發送通知
20 self.localNoti.alertBody = @"wolfhous接收到了5條新消息";
21 self.localNoti.alertTitle = @"title內容";
22 self.localNoti.soundName = @"close.wav";//通知聲音
23 self.localNoti.alertAction = @"鎖屏狀態下的通知內容(灰階狀態顯示)";
24 self.localNoti.applicationIconBadgeNumber = 5;//UIUserNotificationTypeBadge(程序角標
25 [[UIApplication sharedApplication]scheduleLocalNotification:self.localNoti];
26
27 }
28
29 //取消本地通知
30 - (IBAction)cancelBut:(id)sender {
31 NSArray *notes = [UIApplication sharedApplication].scheduledLocalNotifications;
32 NSLog(@"%@",notes);
33 //取消某個通知
34 [[UIApplication sharedApplication]cancelLocalNotification:self.localNoti];
35 // 取消所有通知
36 // [UIApplication sharedApplication]cancelAllLocalNotifications;
37 }
38
39 /**
40 * 接收到通知后,如果沒有點擊通知進入程序,程序角標是不會變化的
41 所以我在此設置了一個but,取消程序角標
42 */
43 - (IBAction)deleteNum:(id)sender {
44 //假設閱讀了5條通知,就取消5條通知角標.
45 [UIApplication sharedApplication].applicationIconBadgeNumber =0;
46 }
47
48 @end
ViewController.m ------歡迎討論-----關于遠程推送,我會在接下來的博客中補充,先補一張遠程推送機制-------自行補腦-------
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!