gpt4 book ai didi

Android通知打开后重新出现

转载 作者:行者123 更新时间:2023-11-30 01:34:32 25 4
gpt4 key购买 nike

每次发布​​新公告时,我都会发布通知。我从 Firebase 获取公告,所以我在 onChildAdded() 上调用通知函数。我会收到每个新公告的通知。但是,如果没有新的公告,即使打开它,最新的公告也会每隔一段时间重新出现一次。

通知类: 公共(public)类 FirebaseBackgroundService 扩展服务 {

private Firebase f = new Firebase("https://infotrack.firebaseio.com/infotrack/announcements");
Query queryAnnouncements = f.orderByChild("timeStamp").limitToLast(1);
Firebase ref = new Firebase("https://infotrack.firebaseio.com/guests");
Firebase userRef = ref.child("users/"+ref.getAuth().getUid()+"/notifications");

@Override
public IBinder onBind(Intent arg0) {
return null;
}

@Override
public void onCreate() {
super.onCreate();

ChildEventListener handler = new ChildEventListener() {

@Override
public void onChildAdded(final DataSnapshot dataSnapshot, String s) {

final AnnouncementsList announcement = dataSnapshot.getValue(AnnouncementsList.class);
Log.e("announcements", dataSnapshot.getKey());

userRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot notifSnapshot) {
Log.e("check", notifSnapshot.getValue().toString());
if (notifSnapshot != null) {
Notifications notif = notifSnapshot.getValue(Notifications.class);
if(!dataSnapshot.getKey().equals(notif.getAnnouncements())){
buildNotif("Announcement", announcement.getBody(), "New announcement from Infotrack");
userRef.child("announcements").setValue(dataSnapshot.getKey());
}
}
}

@Override
public void onCancelled(FirebaseError firebaseError) { }
});

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {}

@Override
public void onCancelled(FirebaseError firebaseError) {}

};

queryAnnouncements.addChildEventListener(handler);
}

private void buildNotif(String body){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setContentTitle("New Announcement")
.setContentText(body)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setAutoCancel(true)
.setVibrate(new long[] { 1000, 500, 1000, 0, 1000 })
.setOnlyAlertOnce(true);

Intent intent = new Intent(this, Announcements.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent pendingIntent =
PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);

builder.setContentIntent(pendingIntent);

NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(1, builder.build());

}

广播接收器:

public class StartFirebaseAtBoot extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(FirebaseBackgroundService.class.getName()));
}

Android list :

<service
android:name=".FirebaseBackgroundService"
android:exported="false"
android:process=":remote" >
</service>

<receiver android:name=".StartFirebaseAtBoot" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

我将这一行放在 MainActivity 的 onCreate() 方法中:startService(new Intent(this, FirebaseBackgroundService.class));

堆栈跟踪:

    02-14 18:41:25.262 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/announcements: -KAUMTXlrOFvFaj20TNi
02-14 18:41:25.282 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:25.372 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:25.382 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:25.802 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:25.812 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:25.902 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:25.902 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:25.942 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:25.942 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:25.962 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:25.972 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:25.992 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:25.992 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:26.012 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:26.032 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:26.042 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:26.052 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:26.122 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:26.132 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:26.152 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}

它一直持续到最后一行:

02-14 18:41:42.402 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}

最佳答案

当应用程序连接到服务器时,Firebase 将为该位置的每个现有子项调用 onChildAdded()。因此,除非您删除 child ,否则 onChildAdded() 调用的数量将继续增加。

你有两个选择:

  • 在向用户显示通知后从数据库中删除通知

    public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
    AnnouncementsList announcement = snapshot.getValue(AnnouncementsList.class);
    buildNotif(announcement.getBody());
    snapshot.getRef().remove();
    }
  • 标记用户已看到的通知并跳过这些通知

大多数应用程序都选择第一个选项,因为它会自动保持队列较短。

关于Android通知打开后重新出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35273587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com