当一些数据被推送到我的数据库时,我如何从javascript向主题发送通知负载数据



每当一些数据被推送到我的实时数据库时,我都会尝试向我的android推送通知。

  • Myandroid代码(读取通知数据(:

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
    FirebaseMessaging.getInstance().subscribeToTopic("electricity bill").addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void aVoid) {
    String notificationTitle = null, notificationBody = null;
    // Check if message contains a notification payload
    if (remoteMessage.getNotification() != null) {
    notificationTitle = remoteMessage.getNotification().getTitle();
    notificationBody = remoteMessage.getNotification().getBody();
    }
    if(Objects.requireNonNull(notificationBody).contains(pf.getText().toString()))
    sendLocalNotification(notificationTitle, notificationBody);
    }
    });
    }
    private void sendLocalNotification(String notificationTitle, String notificationBody) {
    Intent intent = new Intent(this, record_viewer.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
    PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
    .setAutoCancel(true)   //Automatically delete the notification
    .setSmallIcon(R.mipmap.ic_launcher) //Notification icon
    .setContentIntent(pendingIntent)
    .setContentTitle(notificationTitle)
    .setContentText(notificationBody)
    .setSound(defaultSoundUri);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1234, notificationBuilder.build());
    }
    }
    
  • 我的web代码(这会发送通知数据(:

    在我的HTML页面中,我包含了以下库:

    <script src="https://www.gstatic.com/firebasejs/9.10.0/firebase-app-compat.js"></script>
    <script src="https://www.gstatic.com/firebasejs/9.10.0/firebase-database-compat.js"></script>
    

    和我的javascript:

    const firebaseConfig = {
    apiKey: "AIzW&HFSIHF&WUFSJKFVSIUBbsfhs98fhsifj",
    authDomain: "some domain here",
    projectId: "project id",
    storageBucket: "storage bucket",
    messagingSenderId: "123a23283905",
    databaseURL: "https://some url here",
    appId: "some id here"
    };
    firebase.initializeApp(firebaseConfig);
    for(var k=1; k<table.rows.length; k++){
    if(table.rows[k].cells.item(10).innerHTML != 0){
    firebase.database().ref("ac/0/").push(
    {
    "Current Date":document.getElementsByClassName("curr_date")[k-1].value,
    "EmpNo":table.rows[k].cells.item(1).innerHTML,
    "Flag":0,
    "Prev Date":document.getElementsByClassName("prev_date")[k-1].value,
    "Total charge":table.rows[k].cells.item(10).innerHTML
    }
    )
    }
    }
    //How do I send the notification payload to the topic "electricity bill"?
    

我想通过有效载荷将EmpNoTotal charge数据发送到该主题,如何继续?请帮帮我。

您可以将云函数用于您的用例,特别是实时数据库触发器。如果您只是在侦听数据库中的数据更新,则可以使用onCreate()。您可以在这里检查其他事件处理程序,这样您就会知道什么最适合您的用例。

下面是一个关于如何使用RTDB触发器发送通知的示例代码,供您参考。发送通知时只需使用sendToTopic((,因为您的目标是某个主题。

相关内容

  • 没有找到相关文章

最新更新