Ich plane eine Benachrichtigung, die gestartet wird, wenn eine Nachricht vom Firebase-Dienst empfangen wird, aber das Problem, dass die Benachrichtigung zweimal ausgelöst wird (einmal pünktlich und zweites Mal nach vielleicht 30 min), ist mein Code :Firebase onMessageReceived triggert zweimal
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);
Dies ist 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);
}
Und hier ist Vb.net Senden einer Benachrichtigung Funktion:
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
Gibt es eine bessere Möglichkeit, die Benachrichtigung zu senden?, Gibt es ein Problem innerhalb des AlarmManagers? Danke.