java - Android Studio 通知管理器错误

标签 java android android-studio ibeacon estimote

我编写了一段代码,以便在信标范围内时弹出通知。

我的通知代码如下:

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方法上。这是日志:

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

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

最佳答案

我认为您没有创建通知 channel 。这就是为什么它不显示通知。您必须先创建一个 channel

 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());
    }

关于java - Android Studio 通知管理器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50486017/

相关文章:

android - 在 Android Studio : need step by step explanations 中导入库

java - JLabel 可以有 img 标签吗

java - WebView.findAll 找不到非英文字符

java - Android - JSON 解析未使用 "setText"显示数据

java - 在 Android 上流式传输 .asf

android - 安装 Studio 2.2.1 后出现 "Gradle project refresh failed"错误(无编码!)

如果串行端口保持打开状态,Java 应用程序将不会退出

java - 你怎么能比较android中的字符串大于

java - 断言 true,来自两种方法的变量

android - 如何在警报对话框上有两个单选列表?