極光推送之應(yīng)用內(nèi)外消息推送 核心代碼

#pragma mark -極光推送

#define kNotificationTypes (UIUserNotificationTypeBadge | UIUserNotificationTypeSound |UIUserNotificationTypeAlert)

- (void)initJPushWithOptions:(NSDictionary*)launchOptions {

if([[UIDevicecurrentDevice].systemVersionfloatValue] >=8.0) {

[JPUSHServiceregisterForRemoteNotificationTypes:kNotificationTypes

categories:nil];//可以添加自定義categories

}else{

[JPUSHServiceregisterForRemoteNotificationTypes:kNotificationTypes

categories:nil];//categories必須為nil

}

//啟動SDK

[JPUSHServicesetupWithOption:launchOptionsappKey:kJPushAppKey

channel:@"App Store"

apsForProduction:NO

advertisingIdentifier:nil];

}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {

[JPUSHServiceregisterDeviceToken:deviceToken];//注冊DeviceToken

}

#pragma mark -應(yīng)用外接收推送信息JPush

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {

NSDictionary*aps = [userInfovalueForKey:@"aps"];//取得APNs標(biāo)準(zhǔn)信息內(nèi)容

NSString*alert = [apsvalueForKey:@"alert"];//推送顯示的內(nèi)容key為alert

NSIntegerbadge = [[apsvalueForKey:@"badge"]integerValue];//badge數(shù)量

//NSString *sound = [aps valueForKey:@"sound"]; //播放的聲音

UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:@"通知"message:alertpreferredStyle:UIAlertControllerStyleAlert];

UIAlertAction*okAlertAction = [UIAlertActionactionWithTitle:@"知道了"style:UIAlertActionStyleDefaulthandler:nil];

[alertControlleraddAction:okAlertAction];

[self.window.rootViewControllerpresentViewController:alertControlleranimated:YEScompletion:nil];

[UIApplicationsharedApplication].applicationIconBadgeNumber= badge;

// Required

[JPUSHServicehandleRemoteNotification:userInfo];

}

#pragma mark -添加JPush應(yīng)用內(nèi)接收自定義消息

- (void)addJPushCustomMessage {

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(networkDidReceiveMessage:)name:kJPFNetworkDidReceiveMessageNotificationobject:nil];

}

- (void)networkDidReceiveMessage:(NSNotification*)notification {

NSDictionary*userInfo = [notificationuserInfo];

NSString*content = [userInfovalueForKey:@"content"];//推送顯示的內(nèi)容key為content

UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:@"通知"message:contentpreferredStyle:UIAlertControllerStyleAlert];

UIAlertAction*okAlertAction = [UIAlertActionactionWithTitle:@"知道了"style:UIAlertActionStyleDefaulthandler:nil];

[alertControlleraddAction:okAlertAction];

[self.window.rootViewControllerpresentViewController:alertControlleranimated:YEScompletion:nil];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容