Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.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 onMessageReceived正在触发两次_Android_.net_Vb.net_Firebase_Firebase Cloud Messaging - Fatal编程技术网

Android Firebase onMessageReceived正在触发两次

Android Firebase onMessageReceived正在触发两次,android,.net,vb.net,firebase,firebase-cloud-messaging,Android,.net,Vb.net,Firebase,Firebase Cloud Messaging,我计划在Firebase服务收到消息时启动一个通知,但该通知会触发两次(一次在时间上,第二次在30分钟后)这是我的代码: sharedPreferences = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); // Check if message contains a data payload. if (remoteMessage.getData().si

我计划在Firebase服务收到消息时启动一个通知,但该通知会触发两次(一次在时间上,第二次在30分钟后)这是我的代码:

 sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Intent myIntent = new Intent(MyFirebaseMessagingService.this, MyAlarmService.class);
        pendingIntent = PendingIntent.getService(MyFirebaseMessagingService.this, 0, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        long currentTime = System.currentTimeMillis();

        int hour=sharedPreferences.getInt("Hour",9);
        int min=sharedPreferences.getInt("Min",0);
        Calendar calendar =  Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,hour);
        calendar.set(Calendar.MINUTE,min);



        alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , pendingIntent);
这是MyAlarmService onStart命令:

 public int onStartCommand(Intent intent, int flags, int startId) {


        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setSmallIcon(R.mipmap.ic_launcher);
        mBuilder.setContentTitle("Figures");
        mBuilder.setContentText("New Figures has been updated!");
        mBuilder.setAutoCancel(true);
        mBuilder.setSound(uri);
        Intent resultIntent = new Intent(this, LoginActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(LoginActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());


    return super.onStartCommand(intent, flags, startId);
}
下面是Vb.net发送通知功能:

Private Function sendNotification() As Integer
    Try
        Dim url As String = "https://fcm.googleapis.com/fcm/send"
        Dim tRequest As WebRequest = WebRequest.Create(url)

        tRequest.Method = "post"
        tRequest.ContentType = "application/json"
        Dim data = New With { _
            Key .[to] =myTopic, _
                Key .TTL = "109", _
            Key .data = New With { _
                Key .body = "Updated", _
                Key .title = "Daily" _
            } _
        }
        Dim jssons As String = Newtonsoft.Json.JsonConvert.SerializeObject(data)
        Dim bytearray As Byte() = Encoding.UTF8.GetBytes(jssons)
        tRequest.Headers.Add(String.Format("Authorization: key={0}", MyKey))
        tRequest.Headers.Add(String.Format("Sender: id={0}", MySenderId))
        tRequest.ContentLength = bytearray.Length
        tRequest.ContentType = "application/json"
        Using datastream As Stream = tRequest.GetRequestStream()
            datastream.Write(bytearray, 0, bytearray.Length)

            Using tresponse As WebResponse = tRequest.GetResponse

                Using dataStreamResponse As Stream = tresponse.GetResponseStream()

                    Using tReader As StreamReader = New StreamReader(dataStreamResponse)

                        Dim sResponseFromServer As String = tReader.ReadToEnd()

                        Log(sResponseFromServer, w)
                    End Using
                End Using

            End Using
        End Using

    Catch ex As WebException
        Log(ex.Message, w)
        Return ex.Status



    End Try
    Log("Notification sent", w)
    Return 200

End Function 
是否有更好的方式发送通知?AlarmManager是否存在任何问题?
谢谢。

您的代码似乎没有问题。但根据文件:

触发应用生命周期之外的操作的常见场景是将数据与服务器同步。在这种情况下,您可能会尝试使用重复报警。但是,如果您拥有托管应用程序数据的服务器,那么使用谷歌云消息(GCM)和同步适配器是比AlarmManager更好的解决方案。同步适配器为您提供了与AlarmManager相同的所有调度选项,但它为您提供了更大的灵活性。例如,同步可以基于来自服务器/设备的“新数据”消息(有关详细信息,请参阅运行同步适配器)、用户的活动(或不活动)、一天中的时间等。有关何时以及如何使用GCM和同步适配器的详细讨论,请参阅本页顶部的链接视频

资料来源:

也许你可以尝试使用同步适配器。还要检查您是否收到两次通知(我想您没有收到)。或者检查报警是否设置为30分钟的重复周期


希望这能有所帮助。

您的代码似乎还可以。但根据文件:

触发应用生命周期之外的操作的常见场景是将数据与服务器同步。在这种情况下,您可能会尝试使用重复报警。但是,如果您拥有托管应用程序数据的服务器,那么使用谷歌云消息(GCM)和同步适配器是比AlarmManager更好的解决方案。同步适配器为您提供了与AlarmManager相同的所有调度选项,但它为您提供了更大的灵活性。例如,同步可以基于来自服务器/设备的“新数据”消息(有关详细信息,请参阅运行同步适配器)、用户的活动(或不活动)、一天中的时间等。有关何时以及如何使用GCM和同步适配器的详细讨论,请参阅本页顶部的链接视频

资料来源:

也许你可以尝试使用同步适配器。还要检查您是否收到两次通知(我想您没有收到)。或者检查报警是否设置为30分钟的重复周期


希望这有帮助。

从MyAlarmService.onStartCommand()返回START\u NOT\u STICKY,而不是调用super方法。super方法返回START_STICKY,它将在服务被系统终止后重新启动服务,从而重新触发通知


另一个问题是,一般来说,使用服务进行此操作是否是一个好主意。我会尝试一个BroadcastReceiver,它在您完成后不会继续运行。

从MyAlarmService.onStartCommand()返回START\u NOT\u STICKY,而不是调用super方法。super方法返回START_STICKY,它将在服务被系统终止后重新启动服务,从而重新触发通知

另一个问题是,一般来说,使用服务进行此操作是否是一个好主意。我想试试广播接收机,它在你完成后不会继续运行