0

I tried to create a notification that starts a activity by clicking on it and which you can't swipe away.

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("DroidSchool")
                .setContentText("DroidSchool l\u00E4uft im Hintergrund...");

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_FROM_BACKGROUND);

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        
        int mId = 1234567890;
        mNotificationManager.notify(mId, mBuilder.build());

with the code above the notification gets displayed, but nothing happens when I click on it and you can swipe it away.

4

5 回答 5

3

要保留通知,以这种方式用户不能以任何方式单击它,请添加此标志

Notification mNotification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(mId, notification);

要启动 Activity,您必须将此标志用于您的 Intent

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
于 2013-08-30T15:17:27.990 回答
1

这样做:

 NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("DroidSchool")
            .setContentText("DroidSchool l\u00E4uft im Hintergrund...");

    Intent intent = new Intent(YourActivity.this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);


   Notification noti = mBuilder.build();

   noti.flags |= mBuilder.build().FLAG_NO_CLEAR | mBuilder.build().FLAG_ONGOING_EVENT;

   NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
    manager.notify(0, noti);  
于 2013-08-30T15:15:25.057 回答
0

尝试像这样使用

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_UPDATE_CURRENT);

在 MainActivity.java

不要消费你的通知;即,而不是在 onDestro() 下的 oncreate() 调用中调用以下 2 行代码

 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.cancel(mId);
于 2013-08-30T15:16:58.773 回答
0

您需要此处详细说明的前台服务

值得注意的是,只有 android 4.3 添加了您似乎想要的持久通知,并且可以被用户设置覆盖。

于 2013-08-30T15:15:01.123 回答
0

您可以使用NotificationCompat.Builder's .setOngoing(true),像这样使用它:

NotificationCompat.Builder  builder = new NotificationCompat.Builder(context)
                                                     .setAutoCancel(true)
                                                     .setContentTitle("Downloading Something")
                                                     .setContentText("Downloading")
                                                     .setSound(null)
                                                     .setDefaults(0)
                                                     .setOngoing(true);
// Makes it so the notification is clickable but not destroyable by swiping-away or hitting "clear all"
于 2016-07-27T16:52:13.723 回答