iOS整合極光推送 通知 和 自定義訊息
支援的版本
r1.2.5 以後。
功能說明
只有在前端執行的時候才能收到自定義訊息的推送。
從jpush伺服器獲取使用者推送的自定義訊息的內容、標題、附件欄位等。
實現方法
獲取iOS的推送內容需要在delegate類中註冊通知並實現回撥方法。
1、在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions 加入下面的程式碼:
NSLog(@"測試 application didFinishLaunchingWithOptions"
//Required
if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 8.0) {
// categories
[JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories nil
[
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
//Required
// [JPUSHService setupWithOption:launchOptions]
// pushConfig.plist appKey
//
//
BOOL isProduction = NO;
[
apsForProduction:isProduction
advertisingIdentifier:nil];
// ========== 接受自定義訊息 ==============
- (void)viewDidLoad
NSNotificationCenter *defaultCenter = [NSNotificationCenterdefaultCenter];
[defaultCenter addObserver:self
selector:@selector(networkDidReceiveMessage:)
name:kJPFNetworkDidReceiveMessageNotification
object:nil];
- (void) dealloc
NSNotificationCenter *defaultCenter = [NSNotificationCenterdefaultCenter];
[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 = [[NSDateFormatteralloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *currentContent = [NSString
stringWithFormat:
@"收到自定義訊息:%@\ntitle:%@\ncontent:%@\nextra:%@\n",
[NSDateFormatterlocalizedStringFromDate:[NSDatedate]
dateStyle:NSDateFormatterNoStyle
timeStyle:NSDateFormatterMediumStyle],
title, content, [selflogDic: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]) {
returnnil;
}
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 =
[NSPropertyListSerializationpropertyListFromData: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 = [NSJSONSerializationJSONObjectWithData:[userdata dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaveserror:nil];
NSLog(@"遠端推送callidobj:%@",callidobj);
if ([callidobj isKindOfClass:[NSDictionaryclass]]) {
self.callid = [callidobj objectForKey:@"callid"];
}
}
NSLog(@"遠端推送 callid=%@",self.callid);
NSLog(@"遠端推送 userInfo %@ ", userInfo);
[JPUSHServicehandleRemoteNotification:userInfo];
}
#warning 將得到的deviceToken傳給SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
#warning 將獲取到的token傳給SDK,用於蘋果推送訊息使用
[JPUSHServiceregisterDeviceToken:deviceToken];
[[ECDevicesharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
#warning 註冊deviceToken失敗;此處失敗,與SDK無關,一般是您的環境配置或者證書配置有誤
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:NSLocalizedString(@"apns.failToRegisterApns", Fail to register apns)
message:error.description
delegate:nil
cancelButtonTitle:NSLocalizedString(@"ok", @"OK")
otherButtonTitles:nil];
[alert show];
}