如何在通知点击时显示对话框

How to show Dialog onClick of Notification

我正在创建每日通知应用程序,我允许用户在其中设置时间。为了开发此功能,我使用了 AlarmReceiver 和 BroadcastReceiver。

我想在用户单击通知时在对话框 box/Alert 框中显示通知消息。

有人可以帮我实现这个功能吗?

下面是我设置通知消息的代码。

public class AlarmReceiver extends BroadcastReceiver {

String TAG = "AlarmReceiver";

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    if (intent.getAction() != null && context != null) {
        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
            // Set the alarm here.
            Log.d(TAG, "onReceive: BOOT_COMPLETED");
            LocalData localData = new LocalData(context);
            NotificationScheduler.setReminder(context, AlarmReceiver.class, localData.get_hour(), localData.get_min());
            return;
        }
    }

    Log.d(TAG, "onReceive: ");

    //Trigger the notification
    NotificationScheduler.showNotification(context, MainActivity.class,
            "You have New Notification", "check it now?");

    }
}

点击通知后输出应该是这样的...

带有打开 activity 的未决意图的通知代码,activity 应该为该弹出窗口编码

Notification notif = new Notification(R.drawable.ic_launcher,"List of Contacts...", 
System.currentTimeMillis());
Intent notificationIntent = new Intent(context,AllContacts.class);
notificationIntent.putExtra("from", "notification") 
notificationIntent.putExtra("message", "yourmessage") 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,            
notificationIntent, 0);
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);

并在 AllContacts.class 中的 onCreate

if(getIntent.getStringExtras("from").equals("notification")){
   //popup show with message
}
Intent notifyIntent = new Intent(context,YourActivityClassHere.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID, 
            notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

只需让 PendingIntent 打开您的 Activity 之一,让您的 Activity 完全透明,然后打开一个对话框即可。

编辑:如果您想从通知点击事件中打开 Activity:

假设 notif 是您的通知对象:

Intent notificationIntent = new Intent(this.getApplicationContext(), ActivityToStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0);
notif.contentIntent = contentIntent;

更多detail

如下更改您的 AlarmReceiver class

public class AlarmReceiver extends BroadcastReceiver {

String TAG = "AlarmReceiver";
public static String NotificationMsg="You have 5 unwatched videos", NotificationTitle = "Watch them now?";
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    if (intent.getAction() != null && context != null) {
        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
            // Set the alarm here.
            Log.d(TAG, "onReceive: BOOT_COMPLETED");
            LocalData localData = new LocalData(context);
            NotificationScheduler.setReminder(context, AlarmReceiver.class, localData.get_hour(), localData.get_min());
            return;
        }
    }

    Log.d(TAG, "onReceive: ");

    //Trigger the notification
    NotificationScheduler.showNotification(context, NotificationActivity.class,
            NotificationTitle, NotificationMsg);

}
}

然后新建Activity这里我新建了NotificationActivity

public class NotificationActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ShowDialog();
}

@SuppressLint("ResourceAsColor")
public void ShowDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(NotificationActivity.this);
    builder.setMessage("\n" + NotificationMsg);
    TextView title = new TextView(NotificationActivity.this);
    title.setText(NotificationTitle);
    title.setBackgroundColor(Color.DKGRAY);
    title.setPadding(20, 20, 20, 20);
    title.setGravity(Gravity.CENTER);
    title.setTextColor(Color.WHITE);
    title.setTextSize(20);
    builder.setCustomTitle(title)
            .setPositiveButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            //finishAffinity();
                        }
                    });
    builder.show();
}
}

输出在这里 -

Output will be like this

希望这会奏效...请不要忘记接受答案和投票...