Android 带有警报管理器和广播接收器的工作室通知未发布通知
Android studio notification with alarm manager and broadcastreceiver not posting notification
我想让我的应用程序定期发送通知(下面的代码设置为 30 秒进行测试,但我会在工作后将其更改为每月一次)。
我正在关注另一个 SO post.
的代码
代码运行没有错误,但没有发送通知。谁能指出我为什么这不起作用的方向。
这是我的 activity 代码(REQUEST_CODE 设置为 0):
private void handleNotification() {
Log.d(TAG, "jjjj3: " + "one" );
Intent intent = new Intent(HomePage.this, ClassReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(HomePage.this, REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(alarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30 * 1000, pendingIntent);
Log.d(TAG, "jjjj4: " + "two" );
}
这是我的接收器class:
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)
}
}
创建频道后,使用 CHANNEL_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 代码的更多信息,您可以查看此 Create a Notification
我想让我的应用程序定期发送通知(下面的代码设置为 30 秒进行测试,但我会在工作后将其更改为每月一次)。
我正在关注另一个 SO post.
的代码代码运行没有错误,但没有发送通知。谁能指出我为什么这不起作用的方向。
这是我的 activity 代码(REQUEST_CODE 设置为 0):
private void handleNotification() {
Log.d(TAG, "jjjj3: " + "one" );
Intent intent = new Intent(HomePage.this, ClassReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(HomePage.this, REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(alarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30 * 1000, pendingIntent);
Log.d(TAG, "jjjj4: " + "two" );
}
这是我的接收器class:
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)
}
}
创建频道后,使用 CHANNEL_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 代码的更多信息,您可以查看此 Create a Notification