在单击通知时,应打开一个警报对话框

时间:2019-06-27 10:44:32

标签: android notifications

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

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

BroadcastReceiver类:

public class NotificationrActionReceiver extends BroadcastReceiver {

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

1 个答案:

答案 0 :(得分:1)

如果您需要类似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());
    }
}

AboutActivity.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标签集主题中

Styles.xml

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

添加此样式以使活动作为对话框活动。