如何在Android中创建不会在点击后消失的通知?

11
int icon = R.drawable.icon4;        
CharSequence tickerText = "Hello"; // ticker-text
long when = System.currentTimeMillis();         
Context context = getApplicationContext();     
CharSequence contentTitle = "Hello";  
CharSequence contentText = "Hello";      
Intent notificationIntent = new Intent(this, Example.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

这对我不起作用。

我该如何创建一个可点击并跳转到我的应用程序的通知,但当点击后不会消失?


请问如何使用通知建造者(Notification Builder)? - Ciro Santilli OurBigBook.com
5个回答

25

伙计,你应该阅读整个内容而不仅仅是一部分。请认真逐步重新阅读。

// this
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

int icon = R.drawable.icon4;        
CharSequence tickerText = "Hello"; // ticker-text
long when = System.currentTimeMillis();         
Context context = getApplicationContext();     
CharSequence contentTitle = "Hello";  
CharSequence contentText = "Hello";      
Intent notificationIntent = new Intent(this, Example.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

// and this
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);

6
setLatestEventInfo()在API级别11中已被弃用,必须改用Notification.Builder - AnujAroshA
我已经使用了相同的代码来进行通知。现在,如果我想在通知中添加按钮,该如何实现呢?请帮帮我。 - ADT
我遇到了一个问题,我设置图标为绿色,但当通知出现时,它会自动变为白色。 - HUSNAIN SARWAR

12
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent=new Intent(context,MainActivity.class);
PendingIntent  pending=PendingIntent.getActivity(context, 0, intent, 0);
Notification notification;
    if (Build.VERSION.SDK_INT < 11) {
        notification = new Notification(icon, "Title", when);
        notification.setLatestEventInfo(
                context,
                "Title",
                "Text",
                pending);
    } else {
        notification = new Notification.Builder(context)
                .setContentTitle("Title")
                .setContentText(
                        "Text").setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pending).setWhen(when).setAutoCancel(true)
                .build();
    }
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
nm.notify(0, notification);

或者你可以直接从这里下载教程:http://www.demoadda.com/demo/android/how-to-create-local-notification-notification-manager-demo-with-example-android-source-code_26


1
这是正确的吗?您创建了一个PendingIntent但没有使用它。至少在<11时,它应该是setLatestEventInfo的最后一个参数,或者我错了吗? - hgoebl
我有一个问题,我设置了图标为绿色,但当通知出现时,它会自动变成白色。 - HUSNAIN SARWAR

2
如果你正在使用Android 5.0及以上版本,这将变得更加简单,功能也有所改变,但你可以使用相同的代码。
//Some Vars
public static final int NOTIFICATION_ID = 1; //this can be any int


//Building the Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_stat_notification);
builder.setContentTitle("BasicNotifications Sample");
builder.setContentText("Time to learn about notifications!");

NotificationManager notificationManager = (NotificationManager) getSystemService(
            NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());

请确保您处于应用程序上下文中,如果不是,则可能需要传递上下文并按以下方式更改源代码

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
...
..
.

NotificationManager notificationManager = (NotificationManager) context.getSystemService(
            context.NOTIFICATION_SERVICE);

你可以在以下链接查看完整源代码: https://github.com/googlesamples/android-BasicNotifications/blob/master/Application/src/main/java/com/example/android/basicnotifications/MainActivity.java#L73

我有一个问题,我设置了图标为绿色,但当通知出现时,它会自动变成白色。 - HUSNAIN SARWAR

1
这里是代码。
 Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Push Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());

如果您想在Android上获得不会在点击后消失的通知,那么请进行以下设置。
setAutoCancel(false);

0
 if (android.os.Build.VERSION.SDK_INT>16)
    {
        notificationManager.notify(5, notification.build());
    }else
    {
        notificationManager.notify(5, notification.getNotification());
    }

如果要在 android.os.Build.VERSION.SDK_INT<16 中工作,请记得进行一些更改。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接