iOS推送

推送消息

/*

 推送机制

 1.首先是应用程序注册消息推送。

 2.IOSAPNS ServerdeviceToken。应用程序接受deviceToken

 3.应用程序将deviceToken发送给PUSH服务端程序(Provider)

 4.服务端程序向APNS服务发送消息。

 5.APNS服务将消息发送给iPhone应用程序。 

 第一阶段:推送服务器(provider)把要发送的消息、目的iPhone的标识打包,发给APNS

 第二阶段:APNS在自身的已注册Push服务的iPhone列表中,查找有相应标识的iPhone,并把消息发到iPhone

 第三阶段:iPhone把发来的消息传递给相应的应用程序,并且按照设定弹出Push通知。

 */

    //1.注册推送

 

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

{

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])

    {

        [[UIApplication sharedApplication] registerForRemoteNotifications];

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    }

    else

    {

        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;

        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];

    }

}

//2.获取deviceToken

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

{

//#warning 将获取到的token传给容联SDK,用于苹果推送消息使用

    [[ECDevice sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];

    

    NSString *tokenStr = [deviceToken description];

    NSString *pushToken = [[[tokenStr stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];

    [[[RootObject getRootObject] userInfo] setStrDeviceToken:pushToken];


    NSLog(@"设备号pushToken:%@",pushToken);

    [[ZWLog logInstance] writeLogWithParams:@"token= ", pushToken, nil];

}

3.接收并处理推送过来的消息

//如果远程消息发送过来的时候,app正在运行

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

{

    NSLog(@"push:%@",userInfo);

    // 转换成一个本地通知,显示到通知栏,你也可以直接显示出一个alertView,只是那样稍显aggressive:)

    if (userInfo[@"push"]) {

        if (application.applicationState == UIApplicationStateActive) {

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

            NSDictionary *dicPushData = userInfo[@"push"];

//            NSDate *date = [NSDate date];

//            NSDate *dates = [date dateByAddingTimeInterval:60];

            UILocalNotification *localNotification = [[UILocalNotification alloc]init];

            localNotification.userInfo = userInfo;

            localNotification.applicationIconBadgeNumber = [dicApsData[@"badge"] intValue];

            localNotification.soundName = dicApsData[@"sound"];

            localNotification.alertBody = dicApsData[@"alert"];

            localNotification.fireDate = [NSDate date];


            [[UIApplication sharedApplication]scheduleLocalNotification:localNotification];            

}


//用户接收到本地信息时调用

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

{

    [[ZWLog logInstance] writeLogWithParams:@"didReceiveLocalNotification: notification=", notification, nil];

    NSLog(@"推送的消息:%@",notification);

    // "push":{"userName":"13554882785","type":1,"subType":82,"id":90,"title":"报警","content":"[sdgfhds]车辆熄火","soundDetail":"车辆熄火”};

    // "push":({"userName":"13554882785","type":1,"subType":82,"id":90,"title":"报警","content":"[sdgfhds]车辆熄火","soundDetail":"车辆熄火”},{},{});

    if (notification.userInfo[@"push"]) {        

        NSDictionary *dicPush = notification.userInfo[@"push"];

        UIAlertView *messageAlert = [[UIAlertView alloc] initWithTitle:dicPush[@"title"] message:dicPush[@"content"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];

        messageAlert.tag = 55;

        [messageAlert show];

}

}


====================================================================

一般推送的消息体如下:(仅供参考)

msgBody:

 {

 "aps":{"alert":"[sdgfhds]车辆熄火","sound":"82.wav","badge":1},

 "push":{"userName":"13554882785","type":1,"subType":82,"id":90,"title":"报警","content":"[sdgfhds]车辆熄火","soundDetail":"车辆熄火”}

 }


推送的注意事项:【若推送不成功就从以下几个方面找问题】

1.看推送是否注册成功

2.看是否正确的把deviceToken值传给后台

3.检查推送证书是否有效,是否有问题

4.检查当前APP的推送证书是否与后台的证书一致

5.检查推送环境是在测试环境还是在发布环境

(一般在登录时就会把证书和deviceToken传给后台,证书分为测试和发布两种)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wxf_csdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值