// 引入JPush功能所需頭文件
// iOS10註冊APNs所需頭文件
複製代碼
- 在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions裏寫上
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
// 能夠添加自定義categories
// NSSet<UNNotificationCategory *> *categories for iOS10 or later
// NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
[JPUSHService setupWithOption:launchOptions appKey:PushAppKey
channel:@"App Store"
apsForProduction:1
advertisingIdentifier:nil];
複製代碼
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 註冊 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
// iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification API_AVAILABLE(ios(10.0)){
NSDictionary * userInfo = notification.request.content.userInfo;
[self getJHMScanResult:userInfo];
if(@available(iOS 12.1, *)){
}
if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//從通知界面直接進入應用
}else{
//從通知設置界面進入應用
}
}
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler API_AVAILABLE(ios(10.0)){
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
[self getJHMScanResult:userInfo];
if(@available(iOS 12.1, *)){
}
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 須要執行這個方法,選擇是否提醒用戶,有 Badge、Sound、Alert 三種類型能夠選擇設置
}
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler API_AVAILABLE(ios(10.0)){
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
[self getJHMScanResult:userInfo];
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(); // 系統要求執行這個方法
}
複製代碼
- (void)applicationDidBecomeActive:(UIApplication *)application{
application.applicationIconBadgeNumber = 0;
}
複製代碼
[JPUSHService setAlias:[def objectForKey:@"employeesusername"] completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
} seq:1];
複製代碼
//註銷推送
-(void)unRegistPush{
[JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
// if (iResCode == 0)NSLog(@"刪除別名成功");
} seq:1];
}
複製代碼
阿里雲
// iOS10註冊APNs所需頭文件
複製代碼
- (void)registerAPNS:(UIApplication *)application {
[application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes: (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
//APNs註冊成功回調,將返回的deviceToken上傳到CloudPush服務器
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0) {
[CloudPushSDK registerDevice:deviceToken withCallback:^(CloudPushCallbackResult *res) {
if (res.success) {
VLog(@"\n ====== 註冊 deviceToken 成功, deviceToken: %@", [CloudPushSDK getApnsDeviceToken]);
} else {
VLog(@"\n ====== 註冊 deviceToken 失敗, error: %@", res.error);
}
}];
}
//APNs註冊失敗回調
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
VLog(@"\n ====== didFailToRegisterForRemoteNotificationsWithError %@", error);
}
//主動獲取設備通知是否受權 (iOS 10+) 能夠單獨調用(可註釋)
- (void)getNotificationSettingStatus {
if (@available(iOS 10.0, *)) {
[_notificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
VLog(@"\n ====== User authed.");
} else {
VLog(@"\n ====== User denied.");
}
}];
}
}
複製代碼
- (void)initCloudPush {// SDK初始化
[CloudPushSDK asyncInit:PushAppKey appSecret:PushAppSecret callback:^(CloudPushCallbackResult *res) {
if (res.success) {
VLog(@"\n ====== 阿里雲推送初始化成功, deviceId: %@.", [CloudPushSDK getDeviceId]);
} else {
VLog(@"\n ====== 阿里雲推送初始化失敗, error: %@", res.error);
}
}];
}
複製代碼
- (void)registerMessageReceive {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMessageReceived:) name:@"CCPDidReceiveMessageNotification" object:nil];
}
複製代碼
//處理到來推送消息
- (void)onMessageReceived:(NSNotification *)notification {
CCPSysMessage *message = [notification object];
NSString *title = [[NSString alloc] initWithData:message.title encoding:NSUTF8StringEncoding];
NSString *body = [[NSString alloc] initWithData:message.body encoding:NSUTF8StringEncoding];
VLog(@"\n ====== 收到消息 標題: %@, 內容: %@.", title, body);
VLog(@"\n ====== 當前線程 %@",[NSThread currentThread]);
}
// iOS 7+ 不管是前臺仍是後臺只要有遠程推送都會調用
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0) {
if (application.applicationState == UIApplicationStateActive) {// 作相應的判斷是前臺仍是後臺
// 前臺默認不展現推送通知和彈窗,因此用了第三方 https://github.com/pikacode/EBBannerView
[self getJHMScanResult:userInfo];//這個格式跟以前的極光貌似不同,到時候聚合碼反掃要試試看
NSString * content = userInfo[@"aps"][@"alert"][@"body"];
EBBannerView *banner = [EBBannerView bannerWithBlock:^(EBBannerViewMaker *make) {
make.style = EBBannerViewStyleiOS11;//custom system, default is current
make.content = content;
}];
[banner show];
}
}
複製代碼
- 點擊通知將 App 從關閉狀態啓動時,將通知打開回執上報
// 計算點擊 OpenCount
[CloudPushSDK sendNotificationAck:launchOptions];
複製代碼
- (void)applicationDidBecomeActive:(UIApplication *)application{
application.applicationIconBadgeNumber = 0;
}
複製代碼
[CloudPushSDK bindAccount:[def objectForKey:@"employeesusername"] withCallback:^(CloudPushCallbackResult *res) {
}];
複製代碼
[CloudPushSDK unbindAccount:^(CloudPushCallbackResult *res) {
}];
複製代碼