iOSメッセージプッシュ実装

2999 ワード

iOSメッセージプッシュ実装
クライアント
**証明書および権限の申請:***
  •   1 :App       ,        
    
  •   2 :   appID      
    
  •   3 :        P12        
    

  • クライアントコード実装:
  • 1:BundleIdentifierが私たちのプッシュアプリケーションのApp idと一致しなければならないことに注意して、私たちのプッシュのプロジェクトを確立します.
  • 2:AppDelegateでdidFinishLaunchingWithOptions関数に
  • と書く
    - (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
       ……
       //     :  ,  ,  
       [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
       return YES;
    }
    
     
    - (void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken {
       NSLog(@"regisger success:%@",pToken);
       //    , deviceToken            
    }
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
        //       
        NSLog(@"userinfo:%@",userInfo);
       
        NSLog(@"      :%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
    }
    - (void)application:(UIApplication *)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
       NSLog(@"Registfail%@",error);
    }
    
    

    サーバ側
    サービス側の開発の構想はあまり悪くなく、サードパーティ製のパッケージを使って、このパッケージを自分で書くのは面倒です.ここではpushSharpを例に、オープンソースのc#プッシュコード
    キーコードの説明:
    
    var appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../id4test_push_aps_development.p12"));//    
                //IMPORTANT: If you are using a Development provisioning Profile, you must use the Sandbox push notification server 
                //  (so you would leave the first arg in the ctor of ApplePushChannelSettings as 'false')
                //  If you are using an AdHoc or AppStore provisioning profile, you must use the Production push notification server
                //  (so you would change the first arg in the ctor of ApplePushChannelSettings to 'true')
                push.RegisterAppleService(new ApplePushChannelSettings(appleCert, "WEIWEI")); //Extension method
                //Fluent construction of an iOS notification
                //IMPORTANT: For iOS you MUST MUST MUST use your own DeviceToken here that gets generated within your iOS app itself when the Application Delegate
                //  for registered for remote notifications is called, and the device token is passed back to you
                push.QueueNotification(new AppleNotification()
                                           .ForDeviceToken("cde6d75a0fc144b2bd30e10e15855376e4b53d793e6ccf9495787df19ace48d4")//    token
                                           .WithAlert("Hello World!")
                                           .WithBadge(7)
                                           .WithSound("sound.caf"));