2017-12-05 23 views
0

私はAndroidベースのアプリケーションのプッシュ通知を作成しました。これはfirebaseコンソールから送信しようとしたときに動作します。今私が欲しいのは、ユーザーが登録をクリックしてから他のユーザーのための通知を表示するときにプッシュ通知を行うことです。ボタンをクリックしてプッシュ通知を送信する方法

Googleで検索しましたが、例の1つが見つかりませんでした。目標は、私のアプリに新しいユーザー登録があることを他のユーザーに知らせることです。あなたは一人のユーザーがすでに、右登録されている他の皆に登録することをクリックしたときにプッシュを送信するヘルプ

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMessagingService"; 
    public static final int ID_SMALL_NOTIFICATION = 235; 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // ... 

     // TODO(developer): Handle FCM messages here. 

     Log.d(TAG, "From: " + remoteMessage.getFrom()); 

     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
      sendNotification("Hi ini isinya"); 
     } 

     // Check if message contains a notification payload. 
     if (remoteMessage.getNotification() != null) { 
      Log.d(TAG,"Message Notification Title" + remoteMessage.getNotification().getTitle()); 
      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
     } 

     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. See sendNotification method below. 
    } 

    private void sendNotification(String messageBody) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, ID_SMALL_NOTIFICATION, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     String channelId = "fcm_default_channel"; 
     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = 
       new NotificationCompat.Builder(this, channelId) 
         .setSmallIcon(R.drawable.icon) 
         .setContentTitle("FCM Message") 
         .setContentText(messageBody) 
         .setAutoCancel(true) 
         .setSound(defaultSoundUri) 
         .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(ID_SMALL_NOTIFICATION, notificationBuilder.build()); 
    } 

} 

答えて

2

私はあなたが何を意味するか取得していないが、あなたは、あなたが、Firebaseコンソールからあなたサーバーキーをコピーする必要がFirebase Consoleを開くと、プロジェクト

第二選択Volly JSONオブジェクト要求

ファーストを使用することができますプロジェクトでバレーボール依存関係を追加

コンパイル 'com.mcxiaoke.volley:ライブラリ:1.0.19'

は、あなたは、このコードは

private void sendFCMPush() { 

      String SERVER_KEY = YOUR_SERVER_KEY; 
      String msg = "this is test message"; 
      String title = "my title"; 
      String token = FCM_TOKEN; 

      JSONObject obj = null; 
     JSONObject objData = null; 
     JSONObject dataobjData = null; 

     try { 
      obj = new JSONObject(); 
      objData = new JSONObject(); 

      objData.put("body", msg); 
      objData.put("title", title); 
      objData.put("sound", "default"); 
      objData.put("icon", "icon_name"); // icon_name 
      objData.put("tag", token); 
      objData.put("priority", "high"); 

      dataobjData = new JSONObject(); 
      dataobjData.put("text", msg); 
      dataobjData.put("title", title); 

      obj.put("to", token); 
      //obj.put("priority", "high"); 

      obj.put("notification", objData); 
      obj.put("data", dataobjData); 
      Log.e("return here>>", obj.toString()); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

      JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, Constants.FCM_PUSH_URL, obj, 
        new Response.Listener<JSONObject>() { 
         @Override 
         public void onResponse(JSONObject response) { 
          Log.e("True", response + ""); 
         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 
          Log.e("False", error + ""); 
         } 
        }) { 
       @Override 
       public Map<String, String> getHeaders() throws AuthFailureError { 
        Map<String, String> params = new HashMap<String, String>(); 
        params.put("Authorization", "key=" + SERVER_KEY); 
        params.put("Content-Type", "application/json"); 
        return params; 
       } 
      }; 
      RequestQueue requestQueue = Volley.newRequestQueue(this); 
      int socketTimeout = 1000 * 60;// 60 seconds 
      RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); 
      jsObjRequest.setRetryPolicy(policy); 
      requestQueue.add(jsObjRequest); 
} 

・ホープ、このヘルプ

+0

このコードを試しましたが、どのデバイスでも修正を通知しませんでしたか? – Forumesia

+0

どのようなエラーが発生しますか?あなたは決して通知を受けないことを意味しますか? –

+0

登録ボタンをクリックしようとすると、私の主な活動の中にコードが置かれます。どんなデバイスでも通知がありません。 – Forumesia

0

ため

おかげ?。 「...キー= AIza」このような値を持つ「認証」と呼ばれるヘッダで

https://fcm.googleapis.com/fcm/send 

: あなたは、このアドレスへのHTTP POSTリクエストを作成する必要があります。

リクエストの本文で、このようなJSONを送信します。

{ 
    "to": "/topics/foo-bar", 
    "data": { 
    "message": "This is a Firebase Cloud Messaging Topic Message!", 
    } 
} 

トピックを作成して同じトピックに登録する必要があります。

FirebaseMessaging.getInstance().subscribeToTopic("foo-bar"); 

これでうまくいくはずです。

+0

をプッシュするために追加することができますので、ここで、iは、それらのコードを追加する必要がありますか?アンドロイドアプリプロジェクトやPHPファイルの中に? – Forumesia

+0

ユーザーがクリックして登録すると、登録してHTTPリクエストを並行して実行する必要があります –

+0

コードを教えてください。 – Forumesia

関連する問題