您好,登錄后才能下訂單哦!
先看看客戶端:
- (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了。
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= "";
導出證書設置的密碼,沒有設置密碼,就如上
這樣就可以推送了。
***********************/
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。