Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自Android应用程序的Firebase云消息通知_Android_Firebase_Android Studio_Firebase Cloud Messaging - Fatal编程技术网

来自Android应用程序的Firebase云消息通知

来自Android应用程序的Firebase云消息通知,android,firebase,android-studio,firebase-cloud-messaging,Android,Firebase,Android Studio,Firebase Cloud Messaging,我想从一个android设备向多个android设备发送通知,我正在使用FCM发送通知,但问题是我没有收到任何东西。我跟随了一些教程和stackoverflow的链接,但我不明白我做错了什么。我尝试使用改造和okhttp发送通知,但似乎无法生成通知。我可以从Firebase控制台生成通知,但不能从android应用程序生成 使用改型 改装代码 接口 公共接口API{ @标题({ “授权:key=AAAAO-53MSs:APA91bFR…JNL7GjX1D”, “内容类型:应用程序/json” }

我想从一个android设备向多个android设备发送通知,我正在使用FCM发送通知,但问题是我没有收到任何东西。我跟随了一些教程和stackoverflow的链接,但我不明白我做错了什么。我尝试使用改造和okhttp发送通知,但似乎无法生成通知。我可以从Firebase控制台生成通知,但不能从android应用程序生成

使用改型 改装代码 接口
公共接口API{
@标题({
“授权:key=AAAAO-53MSs:APA91bFR…JNL7GjX1D”,
“内容类型:应用程序/json”
})
@邮政(“fcm/发送”)
调用sendNotification(@Body PushNotificationModel PushNotificationModel);
}
解决方案
我能够通过清理和重建项目来解决问题,有时在项目的第一次初始部署时,我无法收到通知,因此关闭应用程序、从多任务栏中删除并重新打开应用程序解决了问题,我能够正确接收通知:)。无论您使用的是截击还是改装,两者都可以。下面的代码将向订阅主题为
topicName
的一组设备发送通知

//send notification to other user
public void sendNotificationToUser() {
        JSONObject mainObj = new JSONObject();
        try {
            mainObj.put("to", "/topics/" + topicName); // topicName = your topic name
            JSONObject notificationObj = new JSONObject();
            notificationObj.put("title", "Add your title");
            notificationObj.put("body", "Body section");
            mainObj.put("notification", notificationObj);

            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,"https://fcm.googleapis.com/fcm/send",
                    mainObj, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {
                @Override
                public Map<String, String> getHeaders() {

                    Map<String, String> header = new HashMap<>();
                    header.put("content-type", "application/json");
                    header.put("authorization", "key="+key);//key = your Database Key
                    return header;
                }
            };
            requestQueue.add(jsonObjectRequest);
            //
        } catch (Exception ignored) {
        }

}
//send notification to other user above
别忘了在舱单上登记。在
manifest>应用程序中添加以下代码

        <service
        android:name=".FCMService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>


在应用程序源代码中添加
firebase key
不是一个好主意。您可以使用
Firebase云功能
[必须升级项目才能使用该功能]

您在onNewToken中订阅了有关FCMService的主题。知道原因了吗?没有必要。当我编写这个代码时,我遵循了一个教程。我可能弄错了。如果没有这一部分的话,检查一下,应该可以让我测试一下代码,我会很快给你回复的。requestQueue.add(jsonObjectRequest);找不到此对象,您在哪里初始化此对象?完成兄弟:)再次感谢:)
public interface API {

    @Headers({
            "Authorization:key=AAAAO-53MSs:APA91bFR...JNL7GjX1D",
            "Content-Type:application/json"
    })
    @POST("fcm/send")
    Call<PushNotificationModel> sendNotification(@Body PushNotificationModel pushNotificationModel);
}
//send notification to other user
public void sendNotificationToUser() {
        JSONObject mainObj = new JSONObject();
        try {
            mainObj.put("to", "/topics/" + topicName); // topicName = your topic name
            JSONObject notificationObj = new JSONObject();
            notificationObj.put("title", "Add your title");
            notificationObj.put("body", "Body section");
            mainObj.put("notification", notificationObj);

            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,"https://fcm.googleapis.com/fcm/send",
                    mainObj, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {
                @Override
                public Map<String, String> getHeaders() {

                    Map<String, String> header = new HashMap<>();
                    header.put("content-type", "application/json");
                    header.put("authorization", "key="+key);//key = your Database Key
                    return header;
                }
            };
            requestQueue.add(jsonObjectRequest);
            //
        } catch (Exception ignored) {
        }

}
//send notification to other user above
public class FCMService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
    //message is recieved. Do whatever you want to do 
}

@Override
public void onNewToken(@NonNull String s) {
            FirebaseMessaging.getInstance().subscribeToTopic("your topic name here").addOnSuccessListener(new OnSuccessListener<Void>() {//subcribe again if some error occured
        @Override
        public void onSuccess(Void aVoid) {
        }
    });
    super.onNewToken(s);
}

}
        <service
        android:name=".FCMService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>