Android studio 带有alarm manager和broadcastreceiver的Android studio通知未发布通知
我正在尝试让我的应用程序定期发送通知(下面的代码设置为30秒进行测试,但我会将其更改为每月一次) 我正在跟踪另一个SO帖子的代码 代码运行时不会出错,但不会发送通知。有人能告诉我为什么这不起作用吗 这是我的活动代码(请求代码设置为0): 这是我的接收机等级:Android studio 带有alarm manager和broadcastreceiver的Android studio通知未发布通知,android-studio,broadcastreceiver,alarmmanager,Android Studio,Broadcastreceiver,Alarmmanager,我正在尝试让我的应用程序定期发送通知(下面的代码设置为30秒进行测试,但我会将其更改为每月一次) 我正在跟踪另一个SO帖子的代码 代码运行时不会出错,但不会发送通知。有人能告诉我为什么这不起作用吗 这是我的活动代码(请求代码设置为0): 这是我的接收机等级: public class ClassReciever extends BroadcastReceiver { private static final String TAG = "Receiver";
public class ClassReciever extends BroadcastReceiver {
private static final String TAG = "Receiver";
@Override
public void onReceive(Context context, Intent intent) {
showNotification(context);
}
public void showNotification(Context context) {
int reqCode = 0;
Log.d(TAG, "jjjj5: " + "three" );
Intent intent = new Intent(context, AddBudget.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, reqCode, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon2)
.setContentTitle("Title")
.setContentText("Some text");
builder.setContentIntent(pendingIntent);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.notify(reqCode, builder.build());
Log.d(TAG, "jjjj6: " + "four" );
}
}
舱单:
<receiver android:name= ".ClassReciever" />
</application>
您的代码很好。这没什么问题。但是,如果您想将Android 8.0及以上版本的设备作为目标,即API 26+,则必须在发送通知之前创建通知通道
private fun createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.channel_name) // this will be displayed in app settings name it wisely
val descriptionText = getString(R.string.channel_description) // this will be displayed in app settings name it wisely
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
创建频道后,使用该频道\u ID
发送如下通知:
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true)
有关此日志或java代码的更多信息,请检查此是否至少可以看到您在showNotification()中编写的日志?是的,我是-我编码到showNotification()中的两个日志每30秒显示在我的Logcat中。所以ShowNotification()方法正在运行-只是没有显示任何通知。您能告诉我们您正在尝试的设备规格吗?您好,我希望我对您的理解是正确的,您是指我正在运行应用程序的手机的品牌等吗?我用的是三星a 30。如果我误解了你的意思,或者你需要更多的细节,请告诉我。谢谢你,这就成功了。
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true)