首页 > 解决方案 > 单击通知时,应打开警报对话框

问题描述

单击通知时,需要打开警报对话框吗?

Intent intent = new Intent(this, NotificationrActionReceiver.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pIntent = PendingIntent.getBroadcast(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
// This is for creating notification channel
        createNotificationChannel();
        final NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(MainActivity.this);
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,channelID_Default);
        builder.setContentTitle("System update ready")
                .setContentText("Tap here to learn more")
                .setContentIntent(pIntent)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setOngoing(true);
        notificationCompat.notify(id, builder.build());

广播接收器类:

public class NotificationrActionReceiver extends BroadcastReceiver {

        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            toastMsg("Notification is opening when clicking ");
}}

标签: androidnotifications

解决方案


如果您想要类似 AlertDialog 的功能,那么您也可以使用 Dialog Acitvity 来实现。我编写了小代码,可以展示您在问题中到底想要什么。我附上了 github 和 gif 视频的链接,这些链接可视化了我在代码中所做的事情。

Github:https ://github.com/shahzadafridi/NotificationOpenDialogActivity

演示 Gif:https ://i.imgur.com/cVW6IyS.gifv

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void sendNotification(View view){
         sendMeNotification("Hell this is notification.");
    }

    public void sendMeNotification(String message) {

        NotificationManager manager = (NotificationManager)  getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder notification = null;
        Intent main = new Intent(this, AboutActivity.class);
        main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 001, main, PendingIntent.FLAG_ONE_SHOT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(this.getString(R.string.channel_id), this.getString(R.string.channel_name), importance);
            channel.setDescription("It's a personal channel");
            channel.enableVibration(false);
            channel.enableLights(true);
            channel.setLightColor(Color.RED);
            manager.createNotificationChannel(channel);
            notification = new NotificationCompat.Builder(this, channel.getId());
        } else {
            notification = new NotificationCompat.Builder(this, this.getString(R.string.channel_id));
        }
        Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        notification.setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentTitle(this.getString(R.string.app_name))
                .setContentText(message)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setAutoCancel(true)
                .setSound(sound)
                .setLights(Color.RED, 200, 200)
                .setContentIntent(pendingIntent);

        manager.notify(0, notification.build());
    }
}

关于Activity.java

public class AboutActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
    }
}

AndroidManifest.xml

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".AboutActivity" android:theme="@style/NoTitleDialog"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

在我们在styles.xml 中定义的AboutActivity 标记集主题中

样式.xml

  <style name="NoTitleDialog" parent="@style/Theme.AppCompat.Dialog">
        <item name="windowNoTitle">true</item>
    </style>

添加此样式以将活动制作为对话活动。


推荐阅读