jpush推送格式 swift_极光推送Jpush功能(具体参照官网说明文档,注意此文红色字体)...

1、导入框架

2、

//推送

#import "APService.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

// Required J push功能

//1注册极光

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1

if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {

//可以添加自定义categories

[APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |

UIUserNotificationTypeSound |

UIUserNotificationTypeAlert)

categories:nil];

} else {

//categories 必须为nil

[APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

UIRemoteNotificationTypeSound |

UIRemoteNotificationTypeAlert)

categories:nil];

}

#else

//categories 必须为nil

[APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

UIRemoteNotificationTypeSound |

UIRemoteNotificationTypeAlert)

categories:nil];

#endif

// Required

[APService setupWithOption:launchOptions];

return YES;

}

#pragma mark- J push

- (void)applicationWillEnterForeground:(UIApplication *)application {

[application setApplicationIconBadgeNumber:0];

[application cancelAllLocalNotifications];

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

// Restart any tasks that were paused (or not yet started) while the

// application was inactive. If the application was previously in the

// background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application {

// Called when the application is about to terminate. Save data if

// appropriate. See also applicationDidEnterBackground:.

}

- (void)application:(UIApplication *)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

//    rootViewController.deviceTokenValueLabel.text =

//    [NSString stringWithFormat:@"%@", deviceToken];

//    rootViewController.deviceTokenValueLabel.textColor =

//    [UIColor colorWithRed:0.0 / 255

//                    green:122.0 / 255

//                     blue:255.0 / 255

//                    alpha:1];

//    NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);

[APService registerDeviceToken:deviceToken];

//注册成功以后给自己服务器的registrationID

_registrationID=[NSString stringWithFormat:@"%@",  [APService registrationID]];

}

- (void)application:(UIApplication *)application

didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

DLog(@"did Fail To Register For Remote Notifications With Error: %@", error);

}

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1

- (void)application:(UIApplication *)application

didRegisterUserNotificationSettings:

(UIUserNotificationSettings *)notificationSettings {

}

// Called when your app has been activated by the user selecting an action from

// a local notification.

// A nil action identifier indicates the default action.

// You should call the completion handler as soon as you've finished handling

// the action.

- (void)application:(UIApplication *)application

handleActionWithIdentifier:(NSString *)identifier

forLocalNotification:(UILocalNotification *)notification

completionHandler:(void (^)())completionHandler {

}

// Called when your app has been activated by the user selecting an action from

// a remote notification.

// A nil action identifier indicates the default action.

// You should call the completion handler as soon as you've finished handling

// the action.

- (void)application:(UIApplication *)application

handleActionWithIdentifier:(NSString *)identifier

forRemoteNotification:(NSDictionary *)userInfo

completionHandler:(void (^)())completionHandler {

}

#endif

- (void)application:(UIApplication *)application

didReceiveRemoteNotification:(NSDictionary *)userInfo {

[APService handleRemoteNotification:userInfo];

NSLog(@"收到通知:%@", [self logDic:userInfo]);

//    [rootViewController addNotificationCount];

}

- (void)application:(UIApplication *)application

didReceiveRemoteNotification:(NSDictionary *)userInfo

fetchCompletionHandler:

(void (^)(UIBackgroundFetchResult))completionHandler {

[APService handleRemoteNotification:userInfo];

NSLog(@"收到通知:%@", [self logDic:userInfo]);

//    [rootViewController addNotificationCount];

completionHandler(UIBackgroundFetchResultNewData);

}

- (void)application:(UIApplication *)application

didReceiveLocalNotification:(UILocalNotification *)notification {

[APService showLocalNotificationAtFront:notification identifierKey:nil];

}

// log NSSet with UTF8

// if not ,log will be \Uxxx

- (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;

}

- (void)applicationWillResignActive:(UIApplication *)application

{

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application

{

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值