友盟推送

来源:互联网 发布:大连软件职业学院面积 编辑:程序博客网 时间:2024/05/04 09:03
<span style="font-size:18px;">////  UMMessageHelper.m//  LijeUMMessage////  Created by xiaoyao on 14/11/23.//  Copyright (c) 2014年 lijien. All rights reserved.//#import "UMMessageHelper.h"#import "UMessage.h"#define kUMAppKey @"54704fbcfd98c5af3300640e"@implementation UMMessageHelper+(void)setupWithLaunchOptions:(NSDictionary *)launchoPtions {  [UMessage startWithAppkey:kUMAppKey launchOptions:launchoPtions];  #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];    action1.identifier = @"action1_identifier";    action1.title = @"Accept";    // 当点击的时候启动应用程序    action1.activationMode = UIUserNotificationActivationModeForeground;        UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];    action2.identifier = @"action2_idetifier";    action2.title = @"Reject";    // 当点击的时候不启动程序,在后台处理    action2.activationMode = UIUserNotificationActivationModeBackground;    // 需要进行解锁才能够处理    action2.authenticationRequired = YES;    action2.destructive = YES;        UIMutableUserNotificationCategory *categories = [[UIMutableUserNotificationCategory alloc] init];    categories.identifier = @"category1";    [categories setActions:@[action1,action2] forContext:UIUserNotificationActionContextDefault];        UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|                                                                                             UIUserNotificationTypeBadge|                                                                                             UIUserNotificationTypeSound) categories:[NSSet setWithObject:categories]];    // 注册推送通知    [UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings];  } else {    [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|     UIRemoteNotificationTypeBadge|     UIRemoteNotificationTypeSound];  }  #else  [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|   UIRemoteNotificationTypeBadge|   UIRemoteNotificationTypeSound];  #endif  return;}+ (void)registerDeviceToken:(NSData *)deviceToken {  [UMessage registerDeviceToken:deviceToken];    return;}+ (void)handleRemoteNotification:(NSDictionary *)remoteNotification {  [UMessage didReceiveRemoteNotification:remoteNotification];    return;}+ (void)setAutoAlertView:(BOOL)shouldShow {  [UMessage setAutoAlert:shouldShow];    return;}+ (void)showLocalNotification:(NSDictionary *)userInfo {  // 应用当前处于前台时,需要手动处理  if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {    dispatch_async(dispatch_get_main_queue(), ^{      [UMessage setAutoAlert:NO];      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"推送消息"                                                          message:userInfo[@"aps"][@"alert"]                                                         delegate:nil                                                cancelButtonTitle:@"取消"                                                otherButtonTitles:@"确定", nil];      [alertView show];    });  }  return;}@end////  AppDelegate.m//  LijeUMMessage////  Created by xiaoyao on 14/11/23.//  Copyright (c) 2014年 lijien. All rights reserved.//#import "AppDelegate.h"#import "UMMessageHelper.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    [UMMessageHelper setupWithLaunchOptions:launchOptions];  // Override point for customization after application launch.  self.window.backgroundColor = [UIColor whiteColor];  [self.window makeKeyAndVisible];  return YES;}- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {  [UMMessageHelper registerDeviceToken:deviceToken];      NSLog(@"%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]                stringByReplacingOccurrencesOfString: @">" withString: @""]               stringByReplacingOccurrencesOfString: @" " withString: @""]);  return;}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {  [UMMessageHelper handleRemoteNotification:userInfo];    [UMMessageHelper setAutoAlertView:NO];    if (completionHandler) {    completionHandler(UIBackgroundFetchResultNewData);  }    return;}- (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.}- (void)applicationWillEnterForeground:(UIApplication *)application {  // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (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:.}@end</span>

0 0
原创粉丝点击