zoukankan      html  css  js  c++  java
  • 极光推送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];

    }

  • 相关阅读:
    [Mac] 获取cpu信息
    [gcc warnings] -Wtrigraph warnings
    查看SSD寿命
    [linux] 查看SATA速度和具体设备
    [raspberry p3] [suse] 安装maven
    文本处理例子
    容器中用shell脚本启动如何优雅关闭(传送kill SIGTERM信号)
    kubernetes deployment 使用镜像摘要发布新版本
    Yearning sql工单系统 自动执行工单python脚本
    Kubernetes 企业发行版、容器Pass平台 OpenShift4.3 规划裸机部署
  • 原文地址:https://www.cnblogs.com/niexiaobo/p/4702133.html
Copyright © 2011-2022 走看看