[英]Android Notification not being triggered
I am trying to trigger a basic notification.我正在尝试触发基本通知。 However, when I run my code, no notification appears.
但是,当我运行我的代码时,不会出现任何通知。 This seems strange, considering that I believe I have constructed the notification properly and called the notification manager to launch it (
notificationManager.notify(NOTIFICATION_ID, builder.build()
).这看起来很奇怪,考虑到我相信我已经正确构建了通知并调用了通知管理器来启动它(
notificationManager.notify(NOTIFICATION_ID, builder.build()
)。
MainActivity.java : 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 : 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>
App Gradle :应用 Gradle :
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'
}
For APIs greater than 26 you must create a NotificationChanel before creating your notification and you must add this chanel to the NotificationManger like below:对于大于 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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.