java - 未触发 Android 通知
问题描述
我正在尝试触发基本通知。但是,当我运行我的代码时,不会出现任何通知。这看起来很奇怪,考虑到我相信我已经正确构建了通知并调用了通知管理器来启动它(notificationManager.notify(NOTIFICATION_ID, builder.build()
)。
MainActivity.java:
package com.example.basicalarmsetter;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Notification from MainActivity")
.setContentText("Notification from MainActivity successfully launched!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.basicalarmsetter">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</int
ent-filter>
</activity>
<receiver android:name=".AlarmReceiver" android:process=":remote" />
</application>
</manifest>
应用程序摇篮:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.basicalarmsetter"
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
解决方案
对于大于 26 的 API,您必须在创建通知之前创建 NotificationChanel,并且必须将此通道添加到NotificationManger,如下所示:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//Create the chanel
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, "Notification_name", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Notification from MainActivity")
.setContentText("Notification from MainActivity successfully launched!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
// add the chanel to the notification manger
notificationManager.createNotificationChannel(notificationChannel);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
有关更多信息,请查看官方文档
推荐阅读
- docker - 在 docker compose 中需要一个循环的depends_on
- npm - 如何在 package-lock 和 package json 中应用更改?
- ios - 关于 Swift 中模型结构的可选绑定的问题
- php - Laravel 获取帖子的每个标签类别至少有一个标签
- python - 使用 fftconvolve 在 x 和 y 轴上进行卷积
- ios - 如何从 iOS 13 中的 NFCTagReaderSession NFCISO7816Tag 标签获取 ATR 值或数字
- javascript - Angular Material DatePicker:月份和日期,不包括年份
- sql - 502-Web 服务器在充当网关或代理服务器时收到无效响应
- c# - OpenXml:目录更新给出错误!未定义书签
- javascript - 如何计算印地语中的单词数