在数据库中发生操作时触发Android通知

时间:2016-04-15 17:15:34

标签: android notifications broadcastreceiver alarmmanager

我正在尝试在(Firebase)数据库中进行某些更改时,在我的Android应用程序中向我的用户触发通知。我正在创建一个乘车共享应用程序,所以当找到一个匹配,或者有人加入某人时,骑车,或者骑车'如果被修改,相关的用户应该收到通知,让他们知道。

以下代码是我目前的代码:

    private void scheduleNotification(int delay) {
    Notification notification = getNotification();
    Intent notificationIntent = new Intent(this, NotificationPublisher.class);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    long futureInMillis = SystemClock.elapsedRealtime() + delay;
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}

private Notification getNotification() {
    content = findMatches();
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.pu_train)
                    .setContentTitle("We have a ride match for you!")
                    .setContentText(content);

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ShowRidesActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ShowRidesActivity.class);

    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // mId allows you to update the notification later on.
    //mNotificationManager.notify(1, mBuilder.build());
    mBuilder.setAutoCancel(true);
    return mBuilder.build();
}

在上面的代码中,我创建了一个通知,然后使用alarmManager设置该通知的计划。然后AlarmManager触发我的NotificationPublisher,它看起来像:

public class NotificationPublisher extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";

public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = intent.getParcelableExtra(NOTIFICATION);
    int id = intent.getIntExtra(NOTIFICATION_ID, 0);
    if (ShowRidesActivity.matches.size() > 0) {
        ArrayAdapter adapter = new ArrayAdapter<Ride>(context, android.R.layout.simple_spinner_item, ShowRidesActivity.matches) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                TextView text = (TextView) view.findViewById(android.R.id.text1);
                text.setTextColor(Color.BLACK);
                return view;
            }
        };
        ShowRidesActivity.list.setAdapter(adapter);
    }
    if ((ShowRidesActivity.content) != null && CentralData.notifications)
        notificationManager.notify(id, notification);

}

我已经查看了stackoverflow并用Google搜索,但我无法找到有关如何在某些事件(例如数据库中的更改)上触发通知的任何内容。任何帮助表示赞赏!

0 个答案:

没有答案