博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 远程推送
阅读量:4597 次
发布时间:2019-06-09

本文共 2348 字,大约阅读时间需要 7 分钟。

前期准备工作:到苹果开发者网站上注册AppID、推送证书、描述性证书

代码段实现:

if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {        //IOS8        //创建UIUserNotificationSettings,并设置消息的显示类类型        UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];                [application registerUserNotificationSettings:notiSettings];    }else{               //ios7.0及以下        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge                                       |UIRemoteNotificationTypeSound    |UIRemoteNotificationTypeAlert)];        }

代理方法实现

//会接收来自苹果服务器给你返回的deviceToken,然后你需要将它添加到你本地的推送服务器上。(很重要,决定你的设备能不能接收到推送消息)。- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken{    //注册成功,将deviceToken保存到应用服务器数据库中,在应用服务器端需要    NSLog(@"---Token--%@", pToken);   }//当注册失败时,触发此函数- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{        NSLog(@"Regist fail%@",error);}//- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{    //处理推送消息    NSLog(@"userInfo == %@",userInfo);    NSString *message = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"];        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];        [alert show];}

2.PHP服务端

将simplepush.php这个推送脚本也放在push文件夹中

$message, 'sound' => 'default' ); // Encode the payload as JSON $payload = json_encode($body); // Build the binary notification $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Send it to the server $result = fwrite($fp, $msg, strlen($msg)); if (!$result) echo 'Message not delivered' . PHP_EOL; else echo 'Message successfully delivered' . PHP_EOL; // Close the connection to the server fclose($fp); ?>

deviceToken填写你接收到的token,passPhrase则填写你的ck.pem设置的密码。

此刻就是见证奇迹的时候了

使用终端进入到push文件夹,在终端输入 php simplepush.php

若显示以上提示则表示推送成功了。

转载于:https://www.cnblogs.com/xiangrikui/p/5280116.html

你可能感兴趣的文章
[译]你真的了解外边距折叠吗
查看>>
c#中IList<T>与List<T>
查看>>
python 多线程删除MySQL表
查看>>
ibatis报错
查看>>
SCN学习
查看>>
mysql的启动
查看>>
TCP端口状态说明ESTABLISHED、TIME_WAIT、 CLOSE_WAIT
查看>>
自己电脑能ping别人的,但别人电脑去不能跟我们的电脑通信
查看>>
制作自动化系统安装U盘
查看>>
python模块之xml.etree.ElementTree
查看>>
谷歌模拟
查看>>
【NOI2012】迷失游乐园
查看>>
postgresql 自定义排序
查看>>
任务就绪表OS_PrioGetHighest函数
查看>>
转:大灰狼的汇编视频教程笔记(下)
查看>>
javascript常见的几种事件类型
查看>>
关于大型网站技术演进的思考(八)--存储的瓶颈终篇(8)
查看>>
20+ 个很棒的 jQuery 文件上传插件或教程
查看>>
关于Struts2的多文件上传
查看>>
hosts学习整理
查看>>