Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.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 接收重复的FCM通知_Android_Notifications - Fatal编程技术网

Android 接收重复的FCM通知

Android 接收重复的FCM通知,android,notifications,Android,Notifications,我已经实现了接收FCM通知的代码,我已经设置了代码,一切正常,但问题是通知重复,我检查了post请求,它没有导致任何重复,但onMessageReceived调用了两次,我无法找到原因,我已经检查了一些关于我的问题的其他主题,我要检查我是否有任何触发GCM和FCM的东西,但是一切都好,有人能给出将要发生的事情的建议吗?谢谢 public class NotificationService extends FirebaseMessagingService { private int

我已经实现了接收FCM通知的代码,我已经设置了代码,一切正常,但问题是通知重复,我检查了post请求,它没有导致任何重复,但onMessageReceived调用了两次,我无法找到原因,我已经检查了一些关于我的问题的其他主题,我要检查我是否有任何触发GCM和FCM的东西,但是一切都好,有人能给出将要发生的事情的建议吗?谢谢


 public class NotificationService extends FirebaseMessagingService {

    private int num;
    private RequestQueue requestQueue;

    private String serverUrl = "https://fcm.googleapis.com/fcm/send";
    private String authKey = "key=AIzaSyAwrrIexxxxxxxxxxxxxxxxxxx";

     ///i'm sending data to server in oncreate method
    @Override
    public void onCreate() {
        super.onCreate();

        PostDataQll();
    }

     ///this is where i received message back from server
    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Map<String, String> map = remoteMessage.getData();
        String title = map.get("title");
        String body = map.get("body");

        if (!Objects.requireNonNull(body).equals("")) {
            shownotification(body, title);
        }

    }

    //////show notification 
    void shownotification(String mytime, String awayteam) {

        num = (int) System.currentTimeMillis();

        Notification notification = new NotificationCompat.Builder(getApplicationContext(), 
                 notificationhelper.channelid)
                .setContentText("Now : " + awayteam)
                .setContentTitle(mytime)
                .setChannelId(notificationhelper.channelid)
                .setSmallIcon(R.drawable.ic_notifications)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .build();

        for (int i = 0; i < 1; i++) {
            SystemClock.sleep(3000);
            NotificationManagerCompat.from(this).notify(num, notification);
        }


    }
    /////posting data to the fcm server
    void post(String url, String json) {
        Log.d("TODO", "posted data" + " " + "Yes posted");
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");

        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON, json);
        okhttp3.Request request = new okhttp3.Request.Builder()
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", authKey)
                .url(url)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });

    }
     ////in this method , the user subscribes to the topic 
     //// this is just quickie sample of the data i'm sending to server 
     private void PostDataQll() {


        FirebaseMessaging.getInstance().subscribeToTopic("spanishsport");

        JSONObject jsonObject = new JSONObject();
        JSONObject notification = new JSONObject();

        try {
            jsonObject.put("to", "/topics/spanishsport");
            notification.put("title", "France" + " vs " + "Italy");
            notification.put("body", "20'");
            jsonObject.put("data", notification);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        post("https://fcm.googleapis.com/fcm/send", jsonObject.toString());


    }

公共类NotificationService扩展了FirebaseMessagingService{
私有整数;
私有请求队列请求队列;
专用字符串serverUrl=”https://fcm.googleapis.com/fcm/send";
私有字符串authKey=“key=aizasyawrriexxxxxxxxxxxxxxxx”;
///我正在用oncreate方法向服务器发送数据
@凌驾
public void onCreate(){
super.onCreate();
PostDataQll();
}
///这就是我从服务器收到消息的地方
@凌驾
已接收消息(@NonNull RemoteMessage RemoteMessage)时公共无效{
super.onMessageReceived(remoteMessage);
Map Map=remoteMessage.getData();
字符串title=map.get(“title”);
字符串body=map.get(“body”);
如果(!Objects.requireNonNull(body).equals(“”){
展示通知(正文、标题);
}
}
//////显示通知
void shownotification(字符串mytime、字符串awayteam){
num=(int)System.currentTimeMillis();
Notification Notification=new NotificationCompat.Builder(getApplicationContext(),
notificationhelper.channelid)
.setContentText(“现在:+awayteam)
.setContentTitle(mytime)
.setChannelId(notificationhelper.channelid)
.setSmallIcon(R.drawable.ic_通知)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知))
.build();
对于(int i=0;i<1;i++){
系统时钟。睡眠(3000);
NotificationManagerCompat.from(this).notify(num,notification);
}
}
/////将数据发布到fcm服务器
void post(字符串url、字符串json){
Log.d(“TODO”、“已发布数据”+“”+“已发布是”);
MediaType JSON=MediaType.parse(“application/JSON;charset=utf-8”);
OkHttpClient=新的OkHttpClient();
RequestBody=RequestBody.create(JSON,JSON);
okhttp3.Request-Request=新的okhttp3.Request.Builder()
.addHeader(“内容类型”、“应用程序/json”)
.addHeader(“授权”,authKey)
.url(url)
.职位(机构)
.build();
Call Call=client.newCall(请求);
call.enqueue(新回调(){
@凌驾
公共void onFailure(调用调用,IOE异常){
}
@凌驾
public void onResponse(调用调用、响应响应)引发IOException{
}
});
}
////在此方法中,用户订阅主题
////这只是我发送到服务器的数据的快速示例
私有void PostDataQll(){
FirebaseMessaging.getInstance().subscribeToTopic(“spanishsport”);
JSONObject JSONObject=新的JSONObject();
JSONObject通知=新建JSONObject();
试一试{
jsonObject.put(“to”,“/topics/spanishsport”);
通知。付诸表决(“标题”、“法国”+“vs”+“意大利”);
通知。付诸表决(“正文”、“20”);
jsonObject.put(“数据”,通知);
}捕获(JSONException e){
e、 printStackTrace();
}
职位(”https://fcm.googleapis.com/fcm/send,jsonObject.toString());
}
  • 这是在清单文件中注册服务


我的问题的解决方案是,我打电话给mate@L2_Paver说,我必须从我的片段中发布json对象,希望这能帮助别人。

我的问题的解决方案是,我打电话给mate@L2_Paver说,我必须从我的片段中发布json对象,希望这能帮助别人。
PostDataQ的目的是什么ll()
in
onCreate()
method?Well-PostDatzQll()是一个在oncreate中将数据发布到fcm服务器的函数,我应该将发布函数放在哪里!!我注意到很多人都在问fcm复制问题,我也检查了是否有任何与GCM相关的事情,我什么也没发现..所以我真的不知道为什么会发生这种情况,我也检查了我的权限,没有与GCMYour相关的权限PostDataQll甚至不是动态的。当您将它与函数post放在单独的类中时,您可以在任何地方调用它。PostDataQll post json会触发firebaseMessagingService,该服务调用onCreate方法并调用函数PostDataQll。如果要创建无限循环,请在站在代码流中。试着问问自己为什么我要将
PostDataQll()
放在
onCreate()中
method.Mate谢谢,问题出在oncreate上,所以我把post函数移到我的片段上,在oncreate上调用它,现在没有重复的通知了,谢谢你的帮助
PostDataQll()
oncreate()中的作用是什么
method?Well PostDatzQll()是一个在oncreate中将数据发布到fcm服务器的函数,我应该将发布函数放在哪里!!我注意到很多人都在问fcm复制问题,我也检查了是否有任何与GCM相关的事情,我什么也没发现..所以我真的不知道为什么会发生这种情况,我也检查了我的权限,没有与GCMYour相关的权限PostDataQll不均匀
       <service
            android:name=".notificationui.NotificationService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>