Firebaseメッセージングの初心者向けのニュアンス

Artyom OsipovのHabr記事「 初心者向けのFirebase Cloud Messagingを使用したAndroidでのプッシュ通知 」の公開後、解決できる多くの質問が発生しました。



私は、バイブレーションでプッシュ通知を受け取ることができました。サウンドとステータスバーの通知には大きなアイコンがあり、テキスト全体が近くにあります。 また、アプリケーションがアクティブであるかバックグラウンドで停止しているかは関係ありません。通知をクリックすると、メインアクティビティが常に更新され、通知のパラメーターが保持されます。



したがって、activity_mail.xmlレイアウトで、2つのTextViewを追加します。1つは通知タイトル用、もう1つはテキスト用です。



MainActivityの間:



setContentView(R.layout.activity_main);
      
      





そして



 Button subscribeButton = (Button) findViewById(R.id.subscribeButton);
      
      





すべての行を削除して貼り付けます:



  TextView titleText = (TextView)findViewById(R.id.textView); TextView bodyText = (TextView)findViewById(R.id.textView2); // [START handle_data_extras] if (getIntent().getExtras() != null) { Intent intent = getIntent(); String title = intent.getStringExtra("title"); String body = intent.getStringExtra("body"); titleText.setText(title); bodyText.setText(body); }
      
      





ファイルMyFirebaseMessagingService関数内



 onMessageReceived
      
      





このコードが必要です:



 public void onMessageReceived(RemoteMessage remoteMessage) { sendNotification (remoteMessage.getData().get("title"), remoteMessage.getData().get("body")); }
      
      





と機能



 sendNotification
      
      





この種のコードを受け入れる必要があります:



 private void sendNotification(String messageTitle, String messageBody) { Intent intent = new Intent(this, MainActivity.class); intent.putExtra("title", messageTitle); intent.putExtra("body", messageBody); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_krolik); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_stat_s1) .setLargeIcon(largeIcon) .setColor(Color.parseColor("#4B8A08")) .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody)) .setContentTitle(messageTitle) .setContentText(messageBody) .setAutoCancel(true) .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.circles0)) .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }) .setLights(Color.MAGENTA, 500, 1000) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); }
      
      





どこで



 R.drawable.ic_krolik
      
      





これは通知アイコンであり、



 R.raw.circles0
      
      





これは、着信通知のサウンド用の.mp3ファイルで、rawディレクトリに配置されています。



通知はCURLを介して送信されます。 最初に、テストのために、Raspberry Piターミナルからこれを実行しようとしました。



 curl -s "https://gcm-http.googleapis.com/gcm/send" -H "Authorization: key=AIzaSyB4ZanJWaCay_fXxO5z0jI55T8UOwdGNNQ" -H "Content-Type: application/json" -d '{"to": "fJuOOiTAk4w:APA91bHLrg95cyxodZDULzXU604BXJZUJKBaV8LHCbWPvMUlBhr6tzeD7TfNUp1rOMMObYmnC87OyqwRFbROjZV_pGUtNEh1fGFPDf0ApgljyDrLYEmAneKycVX5jCf8Rm2lpC7MDCss","priority" : "high", "vibrate": 1, "notification": { "title": "Port", "body" : "great", "sound": "circles0.mp3", "color": "#379F00", "icon" : "ic_stat_s"}, "data": {"title" : "3.21.15", "body": 12345678}}'
      
      





しかし、アプリケーションがバックグラウンドにあるか閉じている場合、通知は振動のない切り捨てられたテキスト形式で送信されますが、独自のサウンドとアイコンがあります。 PHPからCURLを送信する必要がありました。 最初にCaspをRaspberry Piにインストールします。



 sudo apt-get install curl libcurl3 libcurl3-dev php5-curl
      
      





PHPファイルを作成します。



 <?php // API access key from Google API's Console define( 'API_ACCESS_KEY', 'AIzaSyB4ZanJWaCay_fXxO5z0jI55T8UOwdGNNQ' ); $registrationIds = array( 'fJuOOiTAk4w:APA91bHLrg95cyxodZDULzXU604BXJZUJKBaV8LHCbWPvMUlBhr6tzeD7TfNUp1rOMMObYmnC87OyqwRFbROjZV_pGUtNEh1fGFPDf0ApgljyDrLYEmAneKycVX5jCf8Rm2lpC7MDCss' ); // prep the bundle $msg = array ( 'message' => 'here is a message. message', 'title' => 'This is a title. title', 'body' => 'This is a subtitle. subtitle, subtitle. subtitle subtitle. subtitle, subtitle. subtitle subtitle. subtitle, subtitle. subtitle subtitle. subtitle', 'tickerText' => 'Ticker text here...Ticker text here...Ticker text here', 'vibrate' => 1, 'sound' => 1, 'largeIcon' => 'large_icon', 'smallIcon' => 'small_icon' ); $fields = array ( 'registration_ids' => $registrationIds, 'data' => $msg ); $headers = array ( 'Authorization: key=' . API_ACCESS_KEY, 'Content-Type: application/json' ); $ch = curl_init(); curl_setopt( $ch,CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/send' ); curl_setopt( $ch,CURLOPT_POST, true ); curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers ); curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false ); curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) ); $result = curl_exec($ch ); curl_close( $ch ); echo $result; ?>
      
      



APA91bHLrg95cyxodZDULzXU604BXJZUJKBaV8LHCbWPvMUlBhr6tzeD7TfNUp1rOMMObYmnC87OyqwRFbROjZV_pGUtNEh1fGFPDf0ApgljyDrLYEmAneKycVX5jCf8Rm2lpC7MDCss'); <?php // API access key from Google API's Console define( 'API_ACCESS_KEY', 'AIzaSyB4ZanJWaCay_fXxO5z0jI55T8UOwdGNNQ' ); $registrationIds = array( 'fJuOOiTAk4w:APA91bHLrg95cyxodZDULzXU604BXJZUJKBaV8LHCbWPvMUlBhr6tzeD7TfNUp1rOMMObYmnC87OyqwRFbROjZV_pGUtNEh1fGFPDf0ApgljyDrLYEmAneKycVX5jCf8Rm2lpC7MDCss' ); // prep the bundle $msg = array ( 'message' => 'here is a message. message', 'title' => 'This is a title. title', 'body' => 'This is a subtitle. subtitle, subtitle. subtitle subtitle. subtitle, subtitle. subtitle subtitle. subtitle, subtitle. subtitle subtitle. subtitle', 'tickerText' => 'Ticker text here...Ticker text here...Ticker text here', 'vibrate' => 1, 'sound' => 1, 'largeIcon' => 'large_icon', 'smallIcon' => 'small_icon' ); $fields = array ( 'registration_ids' => $registrationIds, 'data' => $msg ); $headers = array ( 'Authorization: key=' . API_ACCESS_KEY, 'Content-Type: application/json' ); $ch = curl_init(); curl_setopt( $ch,CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/send' ); curl_setopt( $ch,CURLOPT_POST, true ); curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers ); curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false ); curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) ); $result = curl_exec($ch ); curl_close( $ch ); echo $result; ?>





ターミナルから、通知の送信を開始します。



 php curl.php
      
      





また、アプリケーションがどの状態にあるかは関係ありません。バックグラウンドまたは動作中-サウンドで通知を受け取り、アイコンと通知テキストがすべて収まります。 通知をクリックすると(アプリケーションが実行されている場合)、メインアクティビティが更新され、通知のタイトルと本文が2つのTextViewに配置されます。 アプリケーションがバックグラウンドにある場合、上記の結果で開始されます。



通知をクリックしたときに別のアクティビティを開くには、次のようにします。

Main2Activityという名前の別のアクティビティを作成します。 前のマニフェストで



 </application>
      
      





コードを追加:



 <activity android:name=".Main2Activity"> <intent-filter> <action android:name="OPEN_ACTIVITY_1" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
      
      





CURLに値を追加



 "click_action": "OPEN_ACTIVITY_1"
      
      






All Articles