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

------欢迎讨论-----关于远程推送,我会在接下来的博客中补充,先补一张远程推送机制-------自行补脑-------

 

posted @ 2016-01-16 16:17  wolfhous  阅读(4482)  评论(14编辑  收藏  举报