[英]Firebase onMessageReceived is triggering twice
我正在計划將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 onStartCommand:
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內是否有任何問題? 謝謝。
您的代碼似乎還可以。 但是根據文檔:
在應用程序生命周期之外觸發操作的常見方案是與服務器同步數據。 在這種情況下,您可能會想使用重復警報。 但是,如果您擁有托管應用程序數據的服務器,則將Google Cloud Messaging(GCM)與同步適配器一起使用是比AlarmManager更好的解決方案。 同步適配器為您提供與AlarmManager相同的調度選項,但它為您提供了更大的靈活性。 例如,同步可以基於來自服務器/設備的“新數據”消息(有關詳細信息,請參閱運行同步適配器),用戶的活動(或不活動),一天中的時間等。 有關何時以及如何使用GCM和同步適配器的詳細討論,請參閱此頁面頂部的鏈接視頻。
來源: 安排重復警報文檔
也許您可以嘗試使用同步適配器。 還要檢查您是否兩次收到通知(我認為您沒有)。 或檢查警報是否設置為重復30分鍾。
希望這可以幫助。
從MyAlarmService.onStartCommand()返回START_NOT_STICKY,而不是調用super方法。 超級方法返回START_STICKY,在系統終止服務后,它將重新啟動您的服務,並重新觸發通知。
另一個問題是,為此使用服務是否是一個好主意。 我嘗試使用BroadcastReceiver,完成后將無法繼續運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.