通知管理器错误 Android Studio [英] NotificationManager Error Android Studio

查看:37
本文介绍了通知管理器错误 Android Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个代码来在 Beacon 范围内弹出通知.

I have write a code to have pop-up Notification when in range of Beacon.

我的通知代码如下:

private void showNotification(String message){
        Log.d("Hay8","DCM8");
        Intent intent = new Intent(context, MainActivity.class);
        Log.d("Hay9","DCM9");
        PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        Log.d("Hay10","DCM10");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"default")
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("Notification1")
                .setContentText(message)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent);
        Log.d("Hay11","DCM11");



        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Log.d("Hay12","DCM12");
        notificationManager.notify(NotiID++,builder.build());
    }

我尝试使用日志进行调试,我认为问题出在 NotificationManager 方法上.这是日志:

and i try to debug using log and i think problem is about NotificationManager method. Here is the log:

05-23 17:23:30.774 18668-18668/com.example.user.estimotebeacon D/Hay8: DCM8
05-23 17:23:30.774 18668-18668/com.example.user.estimotebeacon D/Hay9: DCM9
05-23 17:23:30.776 18668-18668/com.example.user.estimotebeacon D/Hay10: DCM10
05-23 17:23:30.780 18668-18668/com.example.user.estimotebeacon D/Hay11: DCM11
05-23 17:23:30.781 18668-18668/com.example.user.estimotebeacon D/Hay12: DCM12
05-23 17:23:30.788 18668-18668/com.example.user.estimotebeacon E/NotificationManager: notifyAsUser: tag=null, id=1, user=UserHandle{0}
05-23 17:23:30.798 18668-18668/com.example.user.estimotebeacon D/Hay20: DCM20
05-23 17:23:30.859 18668-19389/com.example.user.estimotebeacon D/NetworkSecurityConfig: No Network Security Config specified, using platform default
05-23 17:23:32.088 18668-18674/com.example.user.estimotebeacon I/zygote64: Do partial code cache collection, code=28KB, data=21KB
05-23 17:23:32.089 18668-18674/com.example.user.estimotebeacon I/zygote64: After code cache collection, code=28KB, data=21KB
    Increasing code cache capacity to 128KB

所以我的应用程序正在运行但没有任何通知.

So my app is running but without any notification.

推荐答案

我认为您没有创建通知频道.这就是它不显示通知的原因.您必须先创建一个频道

I think you are not created notification channel . That's why it doesn't shows notification. You have to create a channel first

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("com.myApp");
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "com.myApp",
                    "My App",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }

完整的解决方案如下

 private void showNotification(String message){

        Log.d("Hay8","DCM8");
        Intent intent = new Intent(context, MapsActivity.class);
        Log.d("Hay9","DCM9");
        PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        Log.d("Hay10","DCM10");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"default")
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("Notification1")
                .setContentText(message)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent);
        Log.d("Hay11","DCM11");



        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Log.d("Hay12","DCM12");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("com.myApp");
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "com.myApp",
                    "My App",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
        notificationManager.notify(2,builder.build());
    }

这篇关于通知管理器错误 Android Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆