Android 上的触摸通知时显示对​​话框

Show Dialog when touch notification on Android

我已经在 Eclipse 项目上使用 Firebase Cloud Messaging 构建了推送通知。现在我想在触摸状态栏上的通知时使用确定按钮进行对话。

谁能帮帮我?或建议如何处理? 仅供参考,任何时候(当应用程序在后台或前台时)如果我触摸顶部的通知,它会显示一个对话框。

非常感谢。

首先,您需要在通知中使用待定意图,定义如何处理通知点击。

    Intent notificationIntent = new Intent(this, DialogActivity.class);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Notification notification = new Notification.Builder(this)
             // YOUR Notification parameters
            .build();

    notification.contentIntent = pendingNotificationIntent;

看到intent指向一个DialogActivity,所以我们需要创建一个DialogActivity来处理intent。请参阅下面的代码:

public class DailogActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // Get Extra data from intent if you pass something
    // Intent intent = getIntent();
    // Bundle extras = intent.getExtras();

    // Show the popup dialog
    showNewDialog(0);
}

public void showNewDialog(int id) {
    // TODO : Code to show the new dialog
}
}