消息推送处理代码样列

本文介绍了一个iOS应用如何实现Apple Push Notification Service (APNs) 的注册、接收及处理流程。包括设备token的获取并发送给服务器,以及如何处理不同状态下的推送消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.cocoachina.com/bbs/read.php?tid-123567.html


#define push_server @"http://192.168.0.123/push/apns.php"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
    /** 注册推送通知功能, */
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    application.applicationIconBadgeNumber = 0;
    //判断程序是不是由推送服务完成的
    if (launchOptions) {
        NSDictionary* pushNotificationKey = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (pushNotificationKey) {
            application.applicationIconBadgeNumber = 0;
        }
    }
 
}
 
/** 接收从苹果服务器返回的唯一的设备token,然后发送给自己的服务端*/
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString* devices_token = [NSString stringWithFormat:@"%@",deviceToken];
    NSString* devices_name = [[UIDevice currentDevice] name];
    NSString* devices_version = [[UIDevice currentDevice] systemVersion];
    NSString* devices_type = [[UIDevice currentDevice] model];
    NSString* mode = @"Development";
    NSString *strUrl = [NSString stringWithFormat:@"%@?action=registerDevices&devices_token=%@&devices_name=%@&devices_version=%@&devices_type=%@&mode=%@",
                        push_server,devices_token,devices_name,devices_version,devices_type,mode];
    strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strUrl];
     
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    //发送URL请求
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
 
//程序处于启动状态,或者在后台运行时,会接收到推送消息,解析处理
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"\napns -> didReceiveRemoteNotification,Receive Data:\n%@", userInfo);
    //把icon上的标记数字设置为0,
    application.applicationIconBadgeNumber = 0;
    if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
        if(application.applicationState ==UIApplicationStateActive){
            [self alertNotice:@"推送通知" withMSG:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] cancleButtonTitle:@"OK" otherButtonTitle:nil];
        }
        NSString *strUrl = [NSString stringWithFormat:@"%@?action=cleanBadgeNumber&id=%@&badge=%d",
                            push_server,[[userInfo objectForKey:@"aps"] objectForKey:@"id"],0];
        strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:strUrl];
         
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        //发送URL请求
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值