FCM未发送通知

时间:2020-02-04 21:13:37

标签: android firebase firebase-cloud-messaging

我关注了fcm页面,但是我无法发送该通知。我测试了通知键参数,但它不是空的,它是另一台设备的令牌。这是我的发送通知功能。

 private void sendMessage()  {

    String sendMessageText = mSendEditText.getText().toString();
    if(!sendMessageText.isEmpty()){

        try {
            FCMNotification.pushFCMNotification(notificationKey,myName,sendMessageText);
            System.out.print("Entered try");
        } catch (Exception e) {
            System.out.print("Entered catch");
            e.printStackTrace();
        }
        DatabaseReference newMessageDb = mDatabaseChat.push();
        Map newMessage = new HashMap();

        newMessage.put("userName", myName);
        newMessage.put("createdByUser", currentUserId);
        newMessage.put("text", sendMessageText);
        mScrollView.postDelayed(new Runnable() {
            @Override
            public void run() {
                //replace this line to scroll up or down
                mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        }, 100L);
        newMessageDb.setValue(newMessage);

        FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserId).child("Unread").child(mMatchId).setValue(true);

    }

    mSendEditText.setText(null);
    mSendButton.clearFocus();
    mSendEditText.requestFocus();

}

1 个答案:

答案 0 :(得分:0)

这对我有用:

  {
    "id": 2073128511113,
    "email": "email@3@gmail.com",
    "number": "5541"
  }

他们还对接收消息进行了一些更改:

public class FCMNotification {

public final static String AUTH_KEY_FCM = "your_key";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";

public static void pushFCMNotification(String DeviceIdKey) throws Exception {

    String authKey = AUTH_KEY_FCM; // You FCM AUTH key
    String FMCurl = API_URL_FCM;

    URL url = new URL(FMCurl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "key=" + authKey);
    conn.setRequestProperty("Content-Type", "application/json");

    JSONObject data = new JSONObject();
    data.put("to", DeviceIdKey.trim());
    JSONObject info = new JSONObject();
    info.put("title", "FCM Notificatoin Title"); // Notification title
    info.put("text", "Hello First Test notification"); // Notification body
    data.put("notification", info);

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data.toString());
    wr.flush();
    wr.close();

    int responseCode = conn.getResponseCode();
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

}

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    FCMNotification.pushFCMNotification("token_of_the_device");
}
}

}

并将其添加到您的AndroidManifest.xml中:

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

/**
 * Service that is responsible for receiving firebase messages and handling 
   them
 *
 * @author filip.trajkovski
 * @version 1.0
 * @since 1.0
 */

public class FirebaseListenerService extends FirebaseMessagingService {

public static final String TAG = FirebaseListenerService.class.getSimpleName();


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "Message received from: " + remoteMessage.getFrom());
    Log.d(TAG,"----- THIS IS THE MESSAGE RECEIVED ------");
    Log.d(TAG, "Message: " + remoteMessage.getNotification().getBody());

}

@Override
public void onNewToken(String token) {
    super.onNewToken(token);

}