iOS不使用第三方平台,发送推送消息
iOS不使用第三方平台,发送推送消息
先看看客户端:
创新互联是一家专注于成都网站制作、成都网站设计、外贸营销网站建设与策划设计,东明网站建设哪家好?创新互联做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:东明等地区。东明做网站价格咨询:18982081108
需要关注两个点:一是代码部分的DeviceToken获取,且看代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//消息推送支持的类型
UIRemoteNotificationType types =
(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert);
//注册消息推送
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
// Override point for customization after application launch.
return YES;
}
//获取DeviceToken成功
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *pushToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""] ;
NSLog(@"DeviceToken:%@",pushToken);
//这里进行的操作,是将Device Token发送到服务端
}
注:这里用到一个小技巧,怎样把NSData数据内容里面的“<”,">"," "给去掉,得到一个有效的DeviceToken。
//注册消息推送失败
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"Register Remote Notifications error:{%@}",[error localizedDescription]);
}
//处理收到的消息推送
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"Receive remote notification : %@",userInfo);
NSDictionary *aps = [userInfo valueForKey:@"aps"];
NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@"温馨提示"
message:content
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
二是制作带有推送消息的证书
进入苹果开发网站:
选中带有推送服务:
创建成功之后,下载证书双击,在钥匙串就能看到:
右键导出p12文件,可以设置密码,也可以不设,一般不设置。以上证书就OK了。
下面来看看java写的服务器代码:
package com.sdunicom.iphone.apns;
import javapns.back.PushNotificationManager;
import javapns.back.SSLConnectionHelper;
import javapns.data.Device;
import javapns.data.PayLoad;
public class MainSend {
public static void main(String[] args) throws Exception {
try {
String deviceToken = "56378f94d620b0210a9228ea513a4ba2cbe61d0b29143116812da411009c0c9e";
PayLoad payLoad = new PayLoad();
payLoad.addAlert("盛科维的同胞们,大家好");
payLoad.addBadge(1);//消息推送标记数,小红圈中显示的数字。
payLoad.addSound("default");
PushNotificationManager pushManager = PushNotificationManager.getInstance();
pushManager.addDevice("iPhone", deviceToken);
//Connect to APNs
String host= "gateway.sandbox.push.apple.com";
int port = 2195;
String certificatePath= "/Users/wangjinhan/Desktop/最近技术研究/java后台推送程序/developcm.p12";
String certificatePassword= "";
pushManager.initializeConnection(host,port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
//Send Push
Device client = pushManager.getDevice("iPhone");
pushManager.sendNotification(client, payLoad);
pushManager.stopConnection();
pushManager.removeDevice("iPhone");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/***********************
代码有几点要注意:
1.String deviceToken = "56378f94d620b0210a9228ea513a4ba2cbe61d0b29143116812da411009c0c9e";
要发送到对应的设备
2.payLoad.addBadge(1);
消息推送标记数,小红圈中显示的数字。服务器上作一个累计,当点击就计数为了,如果没有查看就一直累加。
3.String certificatePath= "/Users/wangjinhan/Desktop/最近技术研究/java后台推送程序/developcm.p12";
证书的路径,不能出错
4.String certificatePassword= "";
导出证书设置的密码,没有设置密码,就如上
这样就可以推送了。
***********************/
分享题目:iOS不使用第三方平台,发送推送消息
链接URL:http://scjbc.cn/article/isjpjj.html