パッケージ com.home.c2dmtest ;import com.google.android.c2dm.C2DMBaseReceiver ;import android.app.Notification ;import android.app.NotificationManager ;import android.app.PendingIntent ;import android.content.Context ;import android.content.Intent ;import android.util.Log ;パブリック クラス C2DMReceiver は C2DMBaseReceiverを拡張します{パブリック C2DMReceiver ( ) {super ( "<yourmail> @ gmail.com" ) ;}@ オーバーライドpublic void onRegistered ( コンテキストコンテキスト、 文字列 registrationId ) {ログ w ( "onRegistered" 、registrationId ) ;}@ オーバーライドpublic void onUnregistered ( コンテキストコンテキスト) {ログ w ( "onUnregistered" 、 "" ) ;}@ オーバーライドpublic void onError ( コンテキストコンテキスト、 文字列 errorId ) {ログ w ( "onError" 、errorId ) ;}@ オーバーライドprotected void onMessage ( コンテキストコンテキスト、インテントインテント) {ログ w ( "onMessage" 、 "" ) ;}}
<?xml version = "1.0" encoding = "utf-8" ?><マニフェスト xmlns:android = " schemas.android.com/apk/res/android"android:versionCode = "1"android:versionName = "1.0" package = "com.home.c2dmtest" ><アプリケーションandroid:debuggable = "true"android:label = "@ string / app_name" ><アクティビティ android:name = "Main"android:label = "@ string / app_name"android:theme = "@android:style / Theme.NoTitleBar" ><意図フィルター><action android:name = "android.intent.action.MAIN" /><category android:name = "android.intent.category.LAUNCHER" /></ intent-filter ></アクティビティ><service android:name = ".C2DMReceiver" /><受信機android:name = "com.google.android.c2dm.C2DMBroadcastReceiver"android:permission = "com.google.android.c2dm.permission.SEND" ><意図フィルター><action android:name = "com.google.android.c2dm.intent.RECEIVE" /><category android:name = "com.home.c2dmtest" /></ intent-filter ><意図フィルター><action android:name = "com.google.android.c2dm.intent.REGISTRATION" /><category android:name = "com.home.c2dmtest" /></ intent-filter ></受信機></アプリケーション><uses-sdk android:minSdkVersion = "8" /><許可android:name = "com.home.c2dmtest.permission.C2D_MESSAGE"android:protectionLevel = "signature" /><uses-permission android:name = "com.home.c2dmtest.permission.C2D_MESSAGE" /><uses-permission android:name = "com.google.android.c2dm.permission.RECEIVE" /><uses-permission android:name = "android.permission.INTERNET" /><uses-permission android:name = "android.permission.WAKE_LOCK" /></マニフェスト>
C2DMessaging。 register ( this 、 "<yourmail> @ gmail.com" ) ;
C2DMessaging。 登録解除 ( これ ) ;
文字列 ID = C2DMessaging。 getRegistrationId ( これ ) ;
@ オーバーライドprotected void onMessage ( コンテキストコンテキスト、Intent receiveIntent ){文字列データ= receiveIntent。 getStringExtra ( "message" ) ;if ( data ! = null ){ログ w ( 「C2DMReceiver」 、データ) ;Intent intent = new Intent ( this 、Main。Class ) ;意図。 putExtra ( "メッセージ" 、データ) ;NotificationManager mManager = ( NotificationManager )getSystemService ( Context.NOTIFICATION_SERVICE ) ;Notification notification = new Notification ( android。R . Drawable。Ic_dialog_info 、「私のC2DMメッセージ」 、 システム 。 currentTimeMillis ( ) ) ;通知。 setLatestEventInfo (コンテキスト、 「アプリ名」 、 「C2DM通知」 、PendingIntent。 getActivity ( this。getBaseContext ( ) 、 0 、インテント、PendingIntent。 FLAG_CANCEL_CURRENT ) ) ;mManager。 通知 ( 0 、通知) ;}}
@ オーバーライドpublic void onCreate ( Bundle savedInstanceState ) {スーパー 。 onCreate ( savedInstanceState ) ;TextView view = new TextView ( this ) ;文字列メッセージ= getIntent ( ) 。 getStringExtra ( "message" ) ;if (メッセージ== null )ビュー。 setText ( "Hello、World !!!" ) ;他にビュー。 setText ( "Your message:" + message ) ;setContentView (ビュー) ;文字列 ID = C2DMessaging。 getRegistrationId ( これ ) ;if ( id == "" ){C2DMessaging。 register ( this 、 "<yourmail> @ gmail.com" ) ;}}
public boolean sendData (文字列 authToken、文字列 registrationId、文字列の崩壊、文字列キー、文字列値)IOExceptionを スローします {//登録IDを設定しますStringBuilder postDataBuilder = new StringBuilder ( ) ;postDataBuilder。 append ( "registration_id" ) 。append ( "=" ) 。 追加 ( registrationId ) ;// collapse_keyを設定-折りたたみキーが同じ場合にメッセージをグループ化し、//たとえば、デバイスはオフになり、1つのメッセージのみが送信されます。//一度にすべてではありません。postDataBuilder。 append ( "&" ) 。 append ( "collapse_key" ) 。 append ( "=" ) 。追加 (折りたたみ) ;// <data。> <key> = <value>の形式で、送信された日付を追加しますpostDataBuilder。 append ( "&" ) 。 append ( "data。" + key ) 。 append ( "=" ) 。append ( URLEncoder。encode ( value、 "UTF-8" ) ) ;バイト [ ] postData = postDataBuilder。 toString ( ) 。 getBytes ( "UTF-8" ) ;URL url = 新しい URL ( " android.clients.google.com/c2dm/send" ) ;//接続を設定します//プロキシproxy = new Proxy(Proxy.Type.HTTP、new InetSocketAddress( "lazerboy.local"、8080));HttpsURLConnection conn = ( HttpsURLConnection ) url。 openConnection ( / *プロキシ* / ) ;conn。 setDoOutput ( true ) ;conn。 setHostnameVerifier ( this .new MyHostnameVerifier ( ) ) ;conn。 setRequestMethod ( "POST" ) ;conn。 setRequestProperty ( "Content-Type" 、"application / x-www-form-urlencoded; charset = UTF-8" ) ;conn。 setRequestProperty ( "Content-Length" 、Integer。toString ( postData。length ) ) ;conn。 setRequestProperty ( "Authorization" 、 "GoogleLogin auth =" + authToken ) ;OutputStream out = conn。 getOutputStream ( ) ;アウト。 書き込み ( postData ) ;アウト。 閉じる ( ) ;//応答コードを取得します。int responseCode = conn。 getResponseCode ( ) ;if ( responseCode == HttpServletResponse。SC_UNAUTHORIZED ||responseCode == HttpServletResponse。 SC_FORBIDDEN ) {システム アウト 。 printf ( 「無許可-トークンが必要」 ) ;falseを 返し ます 。}if ( responseCode == HttpServletResponse。SC_OK ){システム アウト 。 printf ( "デバイスに送信されたデータ!" ) ;trueを 返し ます 。}システム アウト 。 printf ( "何か間違っている、応答メッセージ:" 、接続getResponseMessage ( ) ) ;falseを 返し ます 。}
Source: https://habr.com/ru/post/J116106/More articles:Ubuntu 11.04用に17の新しい壁紙が選択され、デフォルトのデスクトップ画像がわずかに更新されましたボールまたは同性愛者ではない人はいくらですか2つのチャットボット間の会話と、一方が他方を彼がAIであるとどのように確信させたかウェブカメラとオリエンテーションイベント9か月後のUsbFlashSpeed製品ライフサイクル管理システム:CADを含むインターネットビジネスで成功する10の方法Spree 0.50.0リリースCodeFest ITインフラストラクチャ(WiFi、プロジェクター、ラップトップなど)カザンのpythonでの最初のBars GroupマスタークラスのレポートAll Articles