已添加有关Firestore的通知

时间:2018-07-24 13:35:49

标签: android firebase notifications google-cloud-firestore

我创建了一个项目,在将数据添加到Firestore中时可以接收通知。由于我仍然是新手,所以我不知道如何在firestore中实现对添加数据的通知。我尝试了fcm,但是我不知道如何实现它。

有人可以告诉我如何添加通知吗?我应该放在哪里?

这是我的代码:

Query firstQuery = mFirestore.collection("OnlineBusiness").orderBy("timestamp", Query.Direction.DESCENDING);

    firstQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {


            if (e != null) {

                Log.d(TAG, "Error : " + e.getMessage());

            } else {

                for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {

                    UserSendDetail userSendDetail = doc.getDocument().toObject(UserSendDetail.class);
                    userSendDetailList.add(userSendDetail);
                    userSendDetail.notify();

                    usersListSendDetailAdapter.notifyDataSetChanged();
                    createNotify();
                }
            }

        }
    });


}


private void createNotify() {

    //AlarmService
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.SECOND, 5);

    Intent intent = new Intent("my.wedee.com.inilahapps.AlarmReceiver.DISPLAY_NOTIFICATION");
    PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);
}

1 个答案:

答案 0 :(得分:1)

如果您希望即使关闭应用程序也能正常运行,我会使用FCM和Firebase Cloud Functions来实现。

只要在Firestore中更改了数据,就可以启动功能: https://firebase.google.com/docs/functions/firestore-events#trigger_a_function_when_a_new_document_is_created

在该函数中,然后我将触发推送通知:

https://firebase.google.com/docs/cloud-messaging/admin/send-messages

然后在Android中接收它:

https://firebase.google.com/docs/cloud-messaging/android/receive

希望有帮助!