通过 Android 的 FCM 下游消息 - 没有通知?

FCM Downstream message via Android - No notification?

我尝试通过 android 发送下游消息。我收到响应代码 200 但我没有收到通知。可以发送通知 通过 firebase 控制台,我已经检查过了。

这是我的 Post-请求的代码:

 public class DownstreamMessage extends AsyncTask<String,String,String>
    {
    AsyncResponse delegate = null;
    int responseCode;
    @Override
    protected String doInBackground(String... params)
    {
        HttpURLConnection httpURLConnection = null;
        BufferedReader bufferedReader = null;
        String server_key = "key=12345";
        String client_key;
        String content;
        String content_json_string;


        try
        {
            URL url = new URL("https://fcm.googleapis.com/fcm/send");
            client_key = params[0];

            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            httpURLConnection.setRequestProperty("Authorization", server_key);
            httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.connect();





            JSONObject notification_json_object = new JSONObject();
            try
            {
                notification_json_object.put("body","Hello World");
            }
            catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            JSONObject content_json_object = new JSONObject();
            try
            {
                content_json_object.put("to",client_key);
                    content_json_object.put("notification",notification_json_object);

            }
            catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            content_json_string = content_json_object.toString();

            OutputStream output = httpURLConnection.getOutputStream();
            output.write(content_json_string.getBytes());
            output.flush();
            output.close();

            responseCode = httpURLConnection.getResponseCode();


        }
        catch (ProtocolException e)
        {

        }
        catch (IOException e)
        {

        }

        return "" + responseCode;
    }

    @Override
    protected void onPostExecute(String result)
    {
        delegate.processFinish(result);
    }
}

这是 MyFirebaseMessagingService:

    public class MyFirebaseMessagingService extends FirebaseMessagingService
    {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage)
    {
        sendNotification(remoteMessage);
    }

    public void sendNotification(RemoteMessage remoteMessage)
    {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(remoteMessage.getFrom())
                .setContentText(remoteMessage.getNotification().getBody())
                //.setContentText(remoteMessage.getData().toString())
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
}

这是我的清单:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".MyFirebaseMessagingService">
         <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
         </intent-filter>
    </service>

    <service android:name=".InstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCEID_EVENT"/>
        </intent-filter>
    </service>

</application>

希望你能帮帮我! 最好的祝福, 菲利克斯

看来你在这里犯了一个小错误..

您的清单实例 ID 服务意图过滤器中有错字。检查下面的代码。

替换你的

<action android:name="com.google.firebase.INSTANCEID_EVENT"/>

<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>

然后重试。

注意:

  1. Try exiting / close your app and check from Firebase console. You will receive the notification by default. you don't need to write logic to create notification here.
  2. If your app is running and you received the notification, it will not appear as like above but can be able to print it in a log cat and check.

如果有帮助请告诉我..