极光推送的重要方法

1.获取apn推送内容,并做响应处理 --服务器向客户端推送

(1)如果 App 状态为未运行,此函数将被调用,如果launchOptions包含UIApplicationLaunchOptionsLocalNotificationKey表示用户点击apn 通知导致app被启动运行;如果不含有对应键值则表示 App 不是因点击apn而被启动,可能为直接点击icon被启动或其他。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
// apn 内容获取:NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]

(2)如果 App状态为正在前台或者后台运行,那么此函数将被调用,并且可通过AppDelegate的applicationState是否为UIApplicationStateActive判断程序是否在前台运行。此种情况在此函数中处理

- ( void )application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
// apn内容为userInfo
// 取得 APNs 标准信息内容
    NSDictionary *aps = [userInfo valueForKey:@ "aps" ];
    NSString *content = [aps valueForKey:@ "alert" ]; //推送显示的内容
    NSInteger badge = [[aps valueForKey:@ "badge" ] integerValue];  //badge数量
    NSString *sound = [aps valueForKey:@ "sound" ]; //播放的声音    
     // 取得自定义字段内容
    NSString *customizeField1 = [userInfo valueForKey:@ "customizeField1" ]; //自定义参数,key是自己定义的
    NSLog(@ "content =[%@], badge=[%d], sound=[%@], customize field =[%@]" ,content,badge,sound,customizeField1);
     
     // Required
    [APService handleRemoteNotification:userInfo];

(3)如果是使用 iOS 7 的 Remote Notification 特性那么处理函数需要使用

- ( void )application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:( void (^)(UIBackgroundFetchResult))completionHandler;
// apn内容为userInfo
2.获取应用内推送消息--客户端向服务器推送

获取iOS的推送内容需要在delegate类中注册通知并实现回调方法。

在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions 加入下面的代码:

Object C 代码
1
2
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
- ( void )networkDidReceiveMessage:(NSNotification *)notification {
    NSDictionary * userInfo = [notification userInfo];
    NSString *content = [userInfo valueForKey:@ "content" ];
    NSString *extras = [userInfo valueForKey:@ "extras" ];
    NSString *customizeField1 = [extras valueForKey:@ "customizeField1" ]; //自定义参数,key是自己定义的
 
}
3.setLocalNotification---注册本地通知
+ (UILocalNotification *)setLocalNotification:(NSDate *)fireDate
                                    alertBody:(NSString *)alertBody
                                        badge:( int )badge
                                  alertAction:(NSString *)alertAction
                                identifierKey:(NSString *)notificationKey
                                     userInfo:(NSDictionary *)userInfo
                                    soundName:(NSString *)soundName;
举例:
[APService setLocalNotification:[NSDate dateWithTimeIntervalSinceNow:100] //本地推送触发的时间,fireDate必须大于当前时间
                      alertBody:@ "alert content" //推送弹框的按钮现实的内容(iOS8默认为“打开”,其他默认为“应用”)
                          badge:1//应用角标的数字,如果不需要改变角标传-1
                    alertAction:@ "buttonText"//本地推送需要显示的内容
                  identifierKey:@ "identifierKey"//本地推送标识符
                       userInfo:nil//可用来标识推送和增加附加信息
                      soundName:nil];//本地通知声音名称,空为默认声音
4. showLocalNOtificationAtFront--- API用来 在APP前台运行时,仍然将通知显示出来 。(样式为UIAlertView)
+ ( void )showLocalNotificationAtFront:(UILocalNotification *)notification
                       identifierKey:(NSString *)notificationKey;

API必须放在 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification(AppDelegate.m) 苹果的回调函数下。

  • notification         当前触发的UILocalNotification  
  • notificationKey   过滤不需要前台显示的通知。只有notificationKey标示符的通知才会在前台显示。如果需要全部都显示,该参数传nil。

举例:
- ( void )application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { [APService showLocalNotificationAtFront:notification identifierKey:@ "identifierKey" ]; 
}

5.findLocalNOtificationWithIdentifier ---API 用于获取自定义的identifierKey标示符的UILocationNotification对象

+ (NSArray *)findLocalNotificationWithIdentifier:(NSString *)notificationKey;
  •  notificationKey  获取通知对象的标示符
  • API返回数组,包含所有和identifierKey匹配的LocalNotification对象,如果没找到,则为一个空的数组对象。
如:
NSArray *LocalNotifications = [APService findLocalNotificationWithIdentifier:@ "identifierKey" ];
6.deleteLocalNotification---删除指定的LocalNotification对象
+ ( void )deleteLocalNotification:(UILocalNotification *)localNotification;
7.deleteLocalNotificationWithIdentifierKey-------- 删除指定所有identifierKey标示符的通知对象
举例:[APService deleteLocalNotificationWithIdentifierKey:@ "identifierKey" ]; 
8.clearAllLocalNotification----清除所有注册的通知
举例:
[APService clearAllLocalNotifications];
补充功能:
1. 如何关闭 APN  推送?

关闭推送有以下两种方式关闭:

1.在iOS系统设置的通知设置中更改对应app的推送设置(推荐);

2.在代码中调用 [[UIApplication sharedApplication] unregisterForRemoteNotifications];

对应以上关闭方式的重新打开推送方法:

1.在iOS系统设置的通知设置中修改对应app的推送设置;

2.在代码中重新调用 [APService registerForRemoteNotificationTypes:];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值