iOS does not use third-party platforms to send push messages

4999 단어 iospushinformation
First look at the client:

There are two points to pay attention to: one is to obtain DeviceToken in the code part, and look at the code


- (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 
}
Note: A little trick is used here, how to remove the "<",">",""in the NSData data content to get a valid 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];
}
 


The second is to make a certificate with a push message


Enter the Apple development website:

Check with push service:

After the creation is successful, double-click the downloaded certificate, and you can see it in the keychain:

Right-click to export the p12 file, you can set a password, or you can not set it, generally do not set it. The above certificate is OK.
 
 

Let's take a look at the server code written in 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("Senkewei compatriots, everyone");payLoad.addBadge(1);//The number of message push tags, the number displayed in the small red circle. payLoad.addSound("default");PushNotificationManager pushManager = PushNotificationManager.getInstance();pushManager.addDevice("iPhone", deviceToken);//Connect to APNsString host= "gateway.sandbox.push.apple.com";int port = 2195;String certificatePath= "/Users/wangjinhan/Desktop/Recent Technical Research/java Background Pusher/developcm.p12";String certificatePassword= "";pushManager.initializeConnection(host,port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12) ;//Send PushDevice client = pushManager.getDevice("iPhone");pushManager.sendNotification(client, payLoad);pushManager.stopConnection();
pushManager.removeDevice("iPhone");}catch (Exception e) {e.printStackTrace();}
}}
/************************
There are a few things to note about the code:
1. String deviceToken = "56378f94d620b0210a9228ea513a4ba2cbe61d0b29143116812da411009c0c9e";
to be sent to the corresponding device
2.payLoad.addBadge(1);
The number of message push tags, the number displayed in the small red circle. An accumulation is made on the server. When the click is clicked, it is counted, and if it is not viewed, it is accumulated all the time.
3.String certificatePath= "/Users/wangjinhan/Desktop/Recent technical research/java background push program/developcm.p12";
The path to the certificate, can't go wrong
4.String certificatePassword= "";
Export the password set by the certificate, no password is set, just as above
This can be pushed.
************************/

좋은 웹페이지 즐겨찾기