gpt4 book ai didi

android - 为什么 NotificationManager 不接受我的通知

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:49 25 4
gpt4 key购买 nike

我无法获得 NotificationManagerNotifiy接受我的论点。该方法应该采用 3 个参数。

String        tag
int id
Notification notification

enter image description here

为了构建通知,我使用了 NotificationCompat类,但我什至尝试过Notification.Builder .

import android.app.Notification;
import android.support.v4.app.NotificationCompat;

关于我的构建配置,如下:

compileSdkVersion 23
buildToolsVersion "23.0.2"

minSdkVersion 14
targetSdkVersion 21

编辑:代码记录:

import android.app.Notification;
import android.support.v4.app.NotificationCompat;

private void showNotification(Context context, String category, String title, String text, int tag, boolean ongoing)
{
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
PendingIntent pendingIntent = PendingIntent.getActivity(context, new Random().nextInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle(title);
builder.setContentText(text);
builder.setContentIntent(pendingIntent);
builder.setOngoing(ongoing);
builder.setSmallIcon(R.drawable.ic_launcher);

Notification notification = builder.build();
notificationManager.showNotification(context, category, title, text, new Random().nextInt(), ongoing);
notificationManager.notify(null, 0, notification);
}

最佳答案

使用Compat版本NotificationManagerCompat,初始化notificationManager = NotificationManagerCompat.from(getApplicationContext());。之后,通知管理器 notificationManager.notify(null, 0, notification);

import android.support.v4.app.NotificationCompat;

...

private NotificationManagerCompat notificationManager;

...

// onCreate
notificationManager = NotificationManagerCompat.from(getApplicationContext());

...

// Somewhere in your code
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("I'm a title")
.setContentText("Some text")
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pendingIntent)
.build();

notificationManager.notify(null, 0, notification);

关于android - 为什么 NotificationManager 不接受我的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36593188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com