Supported version
After r1.2.5.
Function Description
Only when the front end is running can you receive the push of custom messages.
Obtain the content, title, attachment field, etc. of the custom message pushed by the user from the jpush server .
Implementation
To get the push content of iOS, you need to register the notification in the delegate class and implement the callback method.
1. In the method- ( BOOL )application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *) launchOptions add the following code:
NSLog(@"测试 application didFinishLaunchingWithOptions");
//Required
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
// categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
//Required
// [JPUSHService setupWithOption:launchOptions]
// pushConfig.plist appKey
//
//
BOOL isProduction = NO;
[JPUSHService setupWithOption:launchOptions appKey:APP_KEY channel:CHANNEL
apsForProduction:isProduction
advertisingIdentifier:nil];
// ========== 接受自定义消息 ==============
- (void)viewDidLoad
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(networkDidReceiveMessage:)
name:kJPFNetworkDidReceiveMessageNotification
object:nil];
- (void) dealloc
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter removeObserver:self
name:kJPFNetworkDidReceiveMessageNotification
object:nil];
2、实现回调方法 networkDidReceiveMessage
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSString *title = [userInfo valueForKey:@"title"];
NSString *content = [userInfo valueForKey:@"content"];
NSDictionary *extra = [userInfo valueForKey:@"extras"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *currentContent = [NSString
stringWithFormat:
@"收到自定义消息:%@\ntitle:%@\ncontent:%@\nextra:%@\n",
[NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterNoStyle
timeStyle:NSDateFormatterMediumStyle],
title, content, [self logDic:extra]];
NSLog(@"%@", currentContent);
// [_messageContents insertObject:currentContent atIndex:0];
//
// NSString *allContent = [NSString
// stringWithFormat:@"%@收到消息:\n%@\nextra:%@",
// [NSDateFormatter
// localizedStringFromDate:[NSDate date]
// dateStyle:NSDateFormatterNoStyle
// timeStyle:NSDateFormatterMediumStyle],
// [_messageContents componentsJoinedByString:nil],
// [self logDic:extra]];
//
// _messageContentView.text = allContent;
// _messageCount++;
// [self reloadMessageCountLabel];
}
- (NSString *)logDic:(NSDictionary *)dic {
if (![dic count]) {
return nil;
}
NSString *tempStr1 =
[[dic description] stringByReplacingOccurrencesOfString:@"\\u"
withString:@"\\U"];
NSString *tempStr2 =
[tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
NSString *tempStr3 =
[[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString *str =
[NSPropertyListSerialization propertyListFromData:tempData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:NULL];
return str;
}
参数描述:
content:获取推送的内容
extras:获取用户自定义参数
customizeField1:根据自定义key获取自定义的value
更多实现参考 SDK下载压缩包中的 demo。
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
NSLog(@"推送的内容:%@",notificationSettings);
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
self.callid = nil;
NSString *userdata = [userInfo objectForKey:@"c"];
NSLog(@"远程推送userdata:%@",userdata);
if (userdata) {
NSDictionary*callidobj = [NSJSONSerialization JSONObjectWithData:[userdata dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"远程推送callidobj:%@",callidobj);
if ([callidobj isKindOfClass:[NSDictionary class]]) {
self.callid = [callidobj objectForKey:@"callid"];
}
}
NSLog(@"远程推送 callid=%@",self.callid);
NSLog(@"远程推送 userInfo %@ ", userInfo);
[JPUSHService handleRemoteNotification:userInfo];
}
#warning 将得到的deviceToken传给SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
#warning 将获取到的token传给SDK,用于苹果推送消息使用
[JPUSHService registerDeviceToken:deviceToken];
[[ECDevice sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
#warning 注册deviceToken失败;此处失败,与SDK无关,一般是您的环境配置或者证书配置有误
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"apns.failToRegisterApns", Fail to register apns)
message:error.description
delegate:nil
cancelButtonTitle:NSLocalizedString(@"ok", @"OK")
otherButtonTitles:nil];
[alert show];
}