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 代码
- (
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---注册本地通知
4. showLocalNOtificationAtFront---
API用来
在APP前台运行时,仍然将通知显示出来
。(样式为UIAlertView)
|
+ (NSArray *)findLocalNotificationWithIdentifier:(NSString *)notificationKey;
|
NSArray *LocalNotifications = [APService findLocalNotificationWithIdentifier:@
"identifierKey"
];
6.deleteLocalNotification---删除指定的LocalNotification对象
|