首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >有警报管理器和广播接收器的Android通知没有发布通知

有警报管理器和广播接收器的Android通知没有发布通知
EN

Stack Overflow用户
提问于 2021-05-14 17:35:41
回答 1查看 218关注 0票数 1

我试图让我的应用程序定期发送通知(下面的代码设置为30秒进行测试,但我会将其更改为每月工作一次)。

我正在跟踪另一篇文章的代码。

代码运行时没有错误,但没有发送任何通知。有人能告诉我为什么这不管用吗?

下面是我的活动代码(REQUEST_CODE设置为0):

代码语言:javascript
代码运行次数: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" );


    }

这是我的接受者课:

代码语言:javascript
代码运行次数:0
运行
复制
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" );


    }



}

清单:

代码语言:javascript
代码运行次数:0
运行
复制
        <receiver android:name= ".ClassReciever" />

    </application>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-18 14:43:06

你的密码没问题。这并没有什么问题。但是,如果您想以Android 8.0及以上的设备(即API 26+ )为目标设备,则必须在传递通知之前创建通知通道。

代码语言:javascript
代码运行次数:0
运行
复制
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来传递通知,如下所示:

代码语言:javascript
代码运行次数:0
运行
复制
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代码的更多信息,请检查此创建通知

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67538657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档