標籤:ios java 推送 伺服器
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 *)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];
}
二是製作帶有推送訊息的認證
進入蘋果開發網站:
650) this.width=650;" class="confluence-embedded-image" src="/download/attachments/5505262/FA4E14B8-81B6-4E73-BD44-E09973B0957A.png?version=1&modificationDate=1409809851313&api=v2" style="margin:0px;padding:0px;border:0px;" alt="FA4E14B8-81B6-4E73-BD44-E09973B0957A.png" />
選中帶有推送服務:
650) this.width=650;" class="confluence-embedded-image" src="/download/attachments/5505262/E2B204DF-1C00-44A5-AFDC-B4D3BB364E3B.png?version=1&modificationDate=1409809937478&api=v2" style="margin:0px;padding:0px;border:0px;" alt="E2B204DF-1C00-44A5-AFDC-B4D3BB364E3B.png" />
建立成功之後,下載認證雙擊,在鑰匙串就能看到:
650) this.width=650;" class="confluence-embedded-image" src="/download/attachments/5505262/79EEF468-92E3-46F8-9681-87F564C4354A.jpeg?version=1&modificationDate=1409810340413&api=v2" style="margin:0px;padding:0px;border:0px;" alt="79EEF468-92E3-46F8-9681-87F564C4354A.jpe" />
右鍵匯出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= "";
匯出認證設定的密碼,沒有設定密碼,就如上
這樣就可以推送了。
***********************/
本文出自 “8830148” 部落格,請務必保留此出處http://8840148.blog.51cto.com/8830148/1548731
iOS不使用第三方平台,發送推送訊息