如何在通知上显示对话框

问题描述 投票:-1回答:3

我正在创建Daily Notification App,我允许用户设置时间。为了开发这个功能,我正在使用AlarmReceiver和BroadcastReceiver。

我想在用户点击通知时在对话框/警报框中显示通知消息。

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

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

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?");

    }
}

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

enter image description here

android notifications alertdialog android-alertdialog
3个回答
0
投票

更改您的AlarmReceiver课程如下

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);

}
}

然后在这里创建新活动我创建了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

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


0
投票

打开活动和该活动的具有待定意图的通知代码应为该弹出窗口编码

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);

在onCreate的AllContacts.class中

if(getIntent.getStringExtras("from").equals("notification")){
   //popup show with message
}

0
投票
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完全透明,然后打开一个Dialog。

编辑:如果您想从通知点击事件中打开一个活动:

假设notif是您的Notification对象:

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

更多detail

© www.soinside.com 2019 - 2024. All rights reserved.