代码之家  ›  专栏  ›  技术社区  ›  N Sharma

在特定条件下触发通知在FireBase上满足

  •  1
  • N Sharma  · 技术社区  · 6 年前

    嗨,我想触发通知那些用户谁添加到购物车项目,并没有在7天内购买。

    我想自动完成。在FireBase上实现这一点的正确方法是什么?

    2 回复  |  直到 6 年前
        1
  •  2
  •   James Poag    6 年前
    const functions = require('firebase-functions');
    var admin = require("firebase-admin");
    admin.initializeApp();
    
    // timestamp the cart with last item added date/time
    exports.timestampCartItem =
      functions.database.ref('/users/{uid}/cart/{item}')
        .onCreate((snapshot, context) => {
          return snapshot.ref.parent('timestamp').set((new Date()).getTime()); // milliseconds
        })
    
    
    
    // Call this function every hour using https://cron-job.org/en/
    const CART_EXPIRE_TIME = Number(7 * 24 * 60 * 60 * 1000); // days * hours * minutes * seconds * milliseconds
    exports.scanZombieCarts = functions.https.onRequest((request, response) => {
    
      const server_time = Number((new Date()).getTime());
      const usersDBRef = admin.database().ref('users');
      const notifyDBRef = admin.database().ref('notify'); // queue to send notifications with FCM
    
      return usersDBRef.once('value')
        .then(users => {
          let zombie_promises = [];
          users.forEach(usersnap => {
            let userid = usersnap.key;
            let user = usersnap.val();
            if (user.hasOwnProperty('cart')) {
              let cart_timestamp = Number(user.cart.timestamp || 0) + CART_EXPIRE_TIME;
              if (cart_timestamp < server_time) {
                zombie_promises.push(
                  notifyDBRef.push({
                    'notification': {
                      'body': `You have ${Object.keys(user.cart).length} items in your Cart.`,
                      'title': 'Sales end soon!'
                    },
                    'token': user.devicetoken
                  })
                );
              }
            }
          })
    
          return Promise.all(zombie_promises);
        })
        .then(() => {
          let elapsed_time = ((new Date()).getTime() - (server_time)) / 1000;
          response.status(200);
          response.send(`<h2>Finished scan of Zombie Carts...${elapsed_time} seconds</h2>`);
          return null;
        })
        .catch(err => {
          response.status(500);
          response.send(`<h2>Scan failed</h2>`);
          console.log(err);
        })
    });
    
    // IMPORTANT:
    // https://console.developers.google.com/apis search for and enable FCM
    exports.sendReminder =
      functions.database.ref('/notify/{message}')
        .onCreate((snapshot, context) => {
          let message = snapshot.val();
    
          let send_and_consume = [
            admin.messaging().send(message), // send message to device
            snapshot.ref.remove()            // consumes message from queue
          ]
    
          return Promise.all(send_and_consume)
            .catch(err => {
              console.log(err); // probably bad token
            })
        })
    

    笔记 这假设当用户打开应用程序时,应用程序会写入一个“users/uid/device token”键,其中设备令牌取自消息。

    请参阅有关启用FCM和cron触发器的内部注释。

    测试 将所有这些添加到index.js文件中,使用 firebase deploy 上传到服务器。

    在控制台中手动编辑firebase db以观察触发器自动添加/更新时间戳。

    使用 firebase serve --only functions 从您的计算机本地测试/调试HTTPS触发器。它将提供一个本地主机链接来运行,您可以在控制台中捕获错误。

        2
  •  0
  •   Mohamed Fawzy    6 年前

    不幸的是,目前在FCM中没有类似的功能,但是您可以在自己的服务器或应用程序中使用以下AlarmManager实现定时推送: 在你的舱单上

    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    

    在你看来

    Calendar sevendayalarm = Calendar.getInstance();
    
                    sevendayalarm.add(Calendar.DATE, 7);
    
                    Intent intent = new Intent(this, AlarmReciever.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 001, intent, 0);
    
                    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP, sevendayalarm.getTimeInMillis(), pendingIntent);
    

    你的警报接收器在哪里?

    public  class AlarmReciever extends BroadcastReceiver{
    
            @Override
            public void onReceive(Context context, Intent intent) {
                // push notification
            }
        }
    

    别忘了在你的舱单上声明收件人

    <receiver android:name=".AlarmReceiver" >
             <intent-filter>
               <action android:name="NOTIFICATION_SERVICE" />
             </intent-filter>
         </receiver>