成功将HTTP POST 200转到FCM但在Android应用中没有通知

时间:2017-05-26 18:29:27

标签: java android firebase-cloud-messaging

这个用Java编写的小程序应该通过firebase云消息发送通知到我的Android应用程序。目前,根据我收到的有限响应代码(200,OK),与FCM的连接似乎很成功。但是,Android应用程序未收到任何通知。

当使用同一服务器密钥的另一个第三方Java应用程序时,通知会在同一个Android应用程序上成功传递,在这种情况下不是。

我认为问题在于设备令牌部分,我不需要将通知发送到特定设备,我希望将通知发送到所有/任何可用设备,只要它到达我的设备

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class AndroidPush {

    /**
     * Replace SERVER_KEY with your SERVER_KEY generated from FCM
     * Replace DEVICE_TOKEN with your DEVICE_TOKEN
     */
    private static String SERVER_KEY = "AAA*******APA91bH2i5FGIhBhMj3YEf0yCq*********************************************************************";
    private static String DEVICE_TOKEN = "caNraerHp9M:APA91bEKyYHV9ymsFh4xXyii****************";


    /**
     * USE THIS METHOD to send push notification
     */
    public static void main(String[] args) throws Exception {
        String title = "My First Notification";
        String message = "Hello, I'm push notification";
        sendPushNotification(title, message);
    }


    /**
     * Sends notification to mobile
     */
    private static void sendPushNotification(String title, String message) throws Exception {
        String pushMessage = "{\"data\":{\"title\":\"" +
                title +
                "\",\"message\":\"" +
                message +
                "\"},\"to\":\"" +
                DEVICE_TOKEN +
                "\"}";
        // Create connection to send FCM Message request.
        URL url = new URL("https://fcm.googleapis.com/fcm/send");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization", "key=" + SERVER_KEY);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        // Send FCM message content.
        OutputStream outputStream = conn.getOutputStream();
        outputStream.write(pushMessage.getBytes());

        System.out.println(conn.getResponseCode());
        System.out.println(conn.getResponseMessage());
    }
}

响应代码: 200 确定

ANDROID APPLICATION

public class MainActivity extends AppCompatActivity {

    // defining the constant for the tag in onTokenRefresh
    private static final String TAG = "TFA_service";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onTokenRefresh();
        //get all server-FCM-Android notifications
        FirebaseMessaging.getInstance().subscribeToTopic("ALL");

    }

    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Toast.makeText(this, refreshedToken, Toast.LENGTH_SHORT).show();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
        System.out.println(refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        System.out.println("Registration.onTokenRefresh TOKEN: " + refreshedToken );
    }

1 个答案:

答案 0 :(得分:0)

我也面临同样的问题。我也没有收到通知,所以我在json制作过程中添加以下代码行,

{
   "content_available": true,
   "priority": "high",
   "to": "efxA1bbRGrE:APA91bGmmYrGQBJ6QrM-LOimnbnwMo3WL9MQxJuyuyYdieph0o_P32oouovPB72SFO6ruyyubZKI-RCLAoE1xKQHZFcyVnVfJp92dUAN3-JUcWISplxjqoMjlAzAV6HIdM0n56yw0FxXr7OX44ZzwJ4N",
   "notification": {
       "title": "Strawberry",
       "body": "Hello This is notification test from FCM"
   }
}
{
   Authorization: key=AAAAf0KIvao: APA91bE9eCULa0POdBP94xEPtUeazF_j2XGFcz6GH49RroNmsAmrWt1QG8WYpDFaIoipRtK0Cdn1_2Vz94OoWUlV-4H2IDxwtXM1hAuxcmXn-tcspEbODzsKpcXJjfhaoK2HWTTLHoZlSender: id=546uy577u1042uyuy98Content-Type: application/json
}

所以只需在java文件中使用上面的代码就可以了。它可能有用。