zoukankan      html  css  js  c++  java
  • 推送

    推送:

    实现消息推送的步骤

    1、注册:为应用程序申请消息推送服务。此时你的设备会向APNs服务器发送注册请求。

    2、APNs服务器接收请求,并将deviceToken返给你设备上的应用程序

    3、客户端应用程序将deviceToken发送给后台服务器程序,后台接收并储存。

    4、后台服务器向APNs服务器发送推送消息

    5、APNs服务器将消息发给deviceToken对应设备上的应用程序

    //注册远程消息推送

        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert  | UIRemoteNotificationTypeBadge| UIRemoteNotificationTypeSound];

    //注册推送

      [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];   

      [application registerForRemoteNotifications];

    //注册成功

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

    1、注册成功会弹出提示框征求用户的同意

     2、当用户选择允许之后会在这个方法里取得设备的deviceToken,然后发送给服务器

    3、测试环境与发布环境所连接的服务器地址是不同的,所获取到的deviceToken值也是不同的。deviceToken与应用无关。

    //注册失败

    -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;

    失败原因:

     1、当用户选择不允许的时候会执行此方法

    2、当使用模拟器的时候会执行此方法

    3、证书问题

    //收到远程消息

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

    想要收到推送消息,就必须要有后台服务器向APNs服务器发请求。

    1、公司自己开发后台服务器程序

    2、采用第三方的后台服务程序,比如:百度云推送

    使用百度推送 

    在 App 启动时注册百度云推送服务,需要提供 Apikey

    [BPush registerChannel:launchOptions apiKey:@"bbdnxkzQcU94eByfrGqeQQCQ" pushMode:BPushModeProduction isDebug:NO];

    // 设置 BPush 的回调

        [BPush setDelegate:self];

    //注册deviceToken

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

    {

        [BPush registerDeviceToken:deviceToken];

    //如果用户有注册delegate并实现onMethod:response:,将会回调该函数,通过method参数来判断返回的方法。

        [BPush bindChannel];// 必须。可以在其它时机调用,只有在该方法返回(通过onMethod:response:回调)绑定成功时,app才能接收到Push消息。一个app绑定成功至少一次即可(如果access token变更请重新绑定)。

        

        //在这里保存token,适当时机上传到服务器

        

    }

    // 当 DeviceToken 获取失败时,系统会回调此方法

    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

    {

        debugLog(@"DeviceToken 获取失败,原因:%@",error);

    }

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

    {

        // App 收到推送通知

        [BPush handleNotification:userInfo];

        NSDictionary *aps = userInfo[@"aps"];

        NSString *str = aps[@"alert"];

        //这里要求服务器将数据信息封好,然后我们根据返回的参数类型跳转到相应的控制器。最好单独给一个推送通知的接口。

        NSString *__isNotice;

        if ([str rangeOfString:@"apply:"].location != NSNotFound) {

            [[NSNotificationCenter defaultCenter] postNotificationName:@"NEWTUISONG" object:@"1"];

            if (!__isNotice) {

                [[NSNotificationCenter defaultCenter]postNotificationName:@"BADVALUE" object:@"1"];

            }

        } else {

            [[NSNotificationCenter defaultCenter] postNotificationName:@"NEWTUISONG" object:@"2"];

        }

      

        debugLog(@"userInfo:%@",userInfo);

    }

    #pragma mark - BPushDelegate的方法

    - (void)onMethod:(NSString*)method response:(NSDictionary*)data

    {

        debugLog(@"%@ %@",method,data);

        globalManager.channelid=[data objectForKey:@"channel_id"];

    }

  • 相关阅读:
    Git 分支管理
    Kubernetes 中文文档
    Docker 命令大全
    Redis 常用命令 大全
    Docker 资源汇总
    Docker 安装 Redis
    JavaScript闭包
    CSS选择器
    JavaScript类型转换
    javascript字符串处理方法
  • 原文地址:https://www.cnblogs.com/PengFei-N/p/4703251.html
Copyright © 2011-2022 走看看