您的位置:首页 > 移动开发 > IOS开发

iOS不使用第三方平台,发送推送消息

2014-09-04 14:40 393 查看

iOS不使用第三方平台,发送推送消息

先看看客户端:

需要关注两个点:一是代码部分的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 *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ NSString *pushToken = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""] ; NSLog(@"DeviceToken:%@",pushToken); //这里进行的操作,是将Device Token发送到服务端}注:这里用到一个小技巧,怎样把NSData数据内容里面的“<”,">"," "给去掉,得到一个有效的DeviceToken。//注册消息推送失败- (void)application:(UIApplication *)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ NSLog(@"Register Remote Notifications error:{%@}",[error localizedDescription]);} //处理收到的消息推送- (void)application:(UIApplication *)applicationdidReceiveRemoteNotification:(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= "";导出证书设置的密码,没有设置密码,就如上这样就可以推送了。***********************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 服务器 iOS 推送