【问题标题】:FCM not sending notificationFCM 未发送通知
【发布时间】:2020-02-04 21:13:37
【问题描述】:

我关注了 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();

}

【问题讨论】:

    标签: android firebase firebase-cloud-messaging


    【解决方案1】:

    这对我有用:

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

    他们在接收消息时也做了一些改变:

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

    }

    并将其添加到您的 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>
    

    【讨论】:

    • 我使用了这个并更新了问题,仍然没有
    • 您是否从 firebase 控制台使用此服务器密钥 -> 云消息传递 -> 服务器密钥?您是否在您的设备上收到了一个令牌,这就是您尝试发送通知的那个。此外,如果您正在使用模拟器,请检查您的互联网连接。
    • 我确实收到了一个 fcm 注册令牌,是的,这就是我正在使用的服务器密钥
    • 如果你也有,请检查我添加的最后一个类。这是您收到消息的班级。
    • 该应用程序完美接收通知,我尝试从我的 ios 应用程序发送通知并且 android 应用程序收到了它,问题是 android 发送通知
    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 2017-02-27
    • 2020-09-17
    • 2022-08-22
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    相关资源
    最近更新 更多