在Firebase通知中添加操作按钮?

时间:2017-06-14 18:50:32

标签: android firebase firebase-cloud-messaging android-notifications

如何在Firebase通知中添加两个操作按钮? 我的通知工作完全正常。我想添加两个动作按钮 - 接受和拒绝 我试图添加按钮,但没有任何代码工作。 这是我的代码。帮助我!

  mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
                FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
                displayFirebaseRegId();
            } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
                String message = intent.getStringExtra("message");
                Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
                txtMessage.setText(message);
            }
        }
    };

    displayFirebaseRegId();
}

private void displayFirebaseRegId() {
    SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
    String regId = pref.getString("regId", null);

    Log.e(TAG, "Firebase reg id: " + regId);

    if (!TextUtils.isEmpty(regId))
        txtRegId.setText("Firebase Reg Id: " + regId);
    else
        txtRegId.setText("Firebase Reg Id is not received yet!");
}

@Override
protected void onResume() {
    super.onResume();

    // register GCM registration complete receiver
    LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
            new IntentFilter(Config.REGISTRATION_COMPLETE));

    // register new push message receiver
    // by doing this, the activity will be notified each time a new message arrives
    LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
            new IntentFilter(Config.PUSH_NOTIFICATION));

    // clear the notification area when the app is opened
    NotificationUtils.clearNotifications(getApplicationContext());
}

@Override
protected void onPause() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
    super.onPause();
}

}

1 个答案:

答案 0 :(得分:0)

我认为您通过注册广播接收器来处理来自CMS的传入通知并清除通知区域,从而走上了正确的道路。

现在,您需要在应用程序中创建通知,向通知添加操作并定义待处理意图以处理操作。然后,您可以使用NotificationManager显示通知。因此,您不会显示CMS通知,而是显示本地创建的CMS通知。

提供了示例here