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

时间:2016-07-12 11:20:01

标签: android notifications firebase-cloud-messaging

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

这是我的Post-Request的代码:

 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>

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

1 个答案:

答案 0 :(得分:2)

似乎你在这里犯了一个小错误..

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

替换

directory

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

然后再试一次。

注意:

  
      
  1. 尝试退出/关闭您的应用并从Firebase控制台查看。您将默认收到通知。你不需要写   在这里创建通知的逻辑。
  2.   
  3. 如果您的应用程序正在运行并且您收到了通知,它将不会像上面那样显示,但可以将其打印在日志中   检查。
  4.   

让我知道它是否有帮助..