简体   繁体   English

FCM 不发送通知

[英]FCM not sending notification

I followed the fcm page however i can't get it to send a notification.我关注了 fcm 页面,但是我无法让它发送通知。 i tested the notification key parameter and it is not empty it is another device's Token.我测试了通知键参数,它不为空,它是另一个设备的令牌。 this is my send notification function.这是我的发送通知功能。

 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();

}

This is what worked for me:这对我有用:

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");
}
}

Also they made some changes in receiving the message:他们还对接收消息进行了一些更改:

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);

}

} }

And add this in your AndroidManifest.xml:并将其添加到您的 AndroidManifest.xml 中:

<service android:name=".cloud.msg.FirebaseListenerService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点JS FCM令牌未向用户发送通知 - Node JS FCM token not sending notification to user 在推送通知 FCM (android) 中发送小文本文件 - Sending small text file in push notification FCM (android) 在云函数 FCM 中向特定用户发送推送通知 - Sending push notification to a specific user in cloud function FCM 解析服务器(自行托管)未通过FCM发送Android通知 - Parse server (self hosted) not sending android notification with FCM 使用带有android的FCM从设备向其他人发送通知 - sending notification from device to an other using FCM with android 使用FCM发送通知,数百个android设备 - Sending notification , couple of hundred android devices , using FCM 向已安装我的应用程序的所有用户发送FCM通知 - sending FCM notification to all users that've installed my application 在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递 - Sending FCM Push Notification in Kotlin Client App - Firebase Cloud Messaging FCM:没有调用onMessageReceived,即使将msg发送到fcm后也没有通知? - FCM : onMessageReceived is not called,notification didn't came even after sending msg to fcm? 如何在向 FCM 服务器发送推送通知时验证 HTTP 发布请求 - How to authenticate HTTP post request while sending push notification to FCM server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM