所以我试着在手机顶部创建一个通知,但是当我运行我的代码时,它什么也不做。甚至没有一个错误。我在这里做错了什么?
public void createNotification(Context ctx) {
SharedPreferences settings = ctx.getApplicationContext().getSharedPreferences(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 0);
String contactName = settings.getString("curName", String.valueOf(0));
String contactEmail = settings.getString("contactEmail", String.valueOf(0));
String contactNumber = settings.getString("curPhone", String.valueOf(0));
String dueAmount = String.valueOf(settings.getInt("amountDue", 0));
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setSmallIcon(R.drawable.myanlogo);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("LETTING YOU KNOW");
builder.setContentText("Your notification content here.");
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
// Will display the notification in the notification bar
notificationManager.notify(1, builder.build());
}
我把它放在一个类里,就像这样:
class ContactRVAdapter extends RecyclerView.Adapter<ContactRVAdapter.ViewHolder>
我在这里调用这个函数:
holder.textDueTomorrow.setVisibility(View.GONE);
if (Integer.valueOf(getFirstDateNumber) == Integer.valueOf(getSecondDateNumber) - 1) {
holder.textDueTomorrow.setVisibility(View.VISIBLE);
createNotification(context);
}
一些建议会很好,因为我已经在这方面工作了一段时间了。
3条答案
按热度按时间2ekbmq321#
看起来您缺少Build.VERSION_CODE.O和更高版本https://developer.android.com/develop/ui/views/notifications/channels的通知通道
anauzrmj2#
下面的香奈儿链接和下面提供的例子沿着把我弄晕了。
这个链接使它更清楚。notification channel is not sending notifications它基本上100%的罚款,他只是忘了调用函数createNotificationChannel()之前创建通知。
jgwigjjp3#
对于Android 8及更高版本,您需要创建通知通道,在通知构建器之前写入,并将相同的通道ID传递给
NotificationCompat.Builder
复制/粘贴:
我已更新此行
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, "CHANNEL_ID");
JAVA语言
Kotlin