【问题标题】:Creating a notification with notification channel gives no effect使用通知通道创建通知无效
【发布时间】:2018-06-20 07:17:53
【问题描述】:

我一直在网上寻找答案,但我自己却找不到答案,尽管阅读了官方 android 文档网站上的开发人员指南和来自互联网各地的代码 sn-ps。

我一直在练习在一个新的干净项目上创建通知,尽管创建了一个通知通道和通知本身,我仍然无法让它运行并收到一条消息:“无法在空通道上发布通知” .但是我正在创建频道,并将其分配给我的通知,就像它到处说的那样。我完全没有想法,老实说,我已经浏览堆栈至少几个小时了,每个答案看起来都一样,但它确实对我没有帮助,所以也许如果有人比初学者更好看我的代码可以告诉我我的代码有什么问题?这很烦人,因为通知是我正在进行的项目的重要组成部分,它阻止了我的前进。

public class MainActivity extends AppCompatActivity {


NotificationCompat.Builder notification;
private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    notification = new NotificationCompat.Builder(this);
    notification.setAutoCancel(true);


    findViewById(R.id.buckysButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            createNotification(view);
        }
    });


}

public void createNotification(View view){
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
        notificationChannel.setDescription("Channel description");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        notificationChannel.enableVibration(true);
        nm.createNotificationChannel(notificationChannel);
    }
        notification.setSmallIcon(R.drawable.ic_priority_high_black_24px);
        notification.setTicker("Ticker");
        notification.setWhen(System.currentTimeMillis());
        notification.setContentTitle("Title");
        notification.setContentText("Body Body Body Body Body Body Body Body Body ");
        notification.setChannel(NOTIFICATION_CHANNEL_ID);
        notification.build();

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setContentIntent(pendingIntent);

        nm.notify(NOTIFICATION_ID, notification.build());
    }}

顺便说一句,我正在为 Android 最低级别 5.1 进行开发,我一直想知道是否真的有必要在我的应用程序中放置通道,但我还是这样做了,因为当我尝试在没有它们的情况下这样做时,我仍然得到完全相同的错误。

编辑:我仍在努力解决这个问题。我是否必须在 gradle 文件中做一些事情才能将两个参数传递给 NotificationCompat.Builder 构造函数?不管我做什么,它都不会让我这样做,我认为这是这里的主要问题。

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)

不为我做,它仍然不想将 channel_id 作为参数。

【问题讨论】:

    标签: android notifications sdk mobile-application channels


    【解决方案1】:

    好吧,也许我来晚了,但是……

    尝试缩短您的 channelId 字符串。

    现在

    private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
    

    试试

    private static final String NOTIFICATION_CHANNEL_ID = "my_channel";
    

    对我来说,这是一个非常奇怪的错误。当 channelId 太长时,它不会在我的设备上显示通知。

    编辑:

    不是太长也不是太短,只是系统禁止了一些字符串。它也没有显示在 Android 通知设置屏幕中。

    【讨论】:

      【解决方案2】:

      我找到了丢失的东西。我使用的是 android studio 2.3.3 而不是最新的 3.0.1,所以我无法通过 Gradle 导入一些新功能。

      之后,我只需将我的依赖类路径更改为 'com.android.tools.build:gradle:3.0.1',问题就消失了。

      感谢您的帮助。

      【讨论】:

        【解决方案3】:
        new NotificationCompat.Builder(this);
        

        已弃用,请尝试使用

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
        

        这里是一个示例代码,创建一个NotificationHelper类如下,

        import android.app.NotificationChannel;
        import android.app.NotificationManager;
        import android.content.Context;
        import android.content.ContextWrapper;
        import android.content.Intent;
        import android.graphics.Bitmap;
        import android.graphics.BitmapFactory;
        import android.graphics.Color;
        import android.provider.Settings;
        import android.support.v4.app.NotificationCompat;
        import android.support.v4.content.ContextCompat;
        
        public class NotificationHelper extends ContextWrapper {
            private NotificationManager mNotificationManager;
            private final String MY_CHANNEL = "my_channel";
            private final long[] vibrationScheme = new long[]{200, 400};
        
            /**
             * Registers notification channels, which can be used later by individual notifications.
             *
             * @param context The application context
             */
            public NotificationHelper(Context context) {
                super(context);
        
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        
                    // Create the channel object with the unique ID MY_CHANNEL
                    NotificationChannel myChannel =
                            new NotificationChannel(
                                    MY_CHANNEL,
                                    getResources().getString(R.string.notification_channel_title),
                                    NotificationManager.IMPORTANCE_DEFAULT);
        
                    // Configure the channel's initial settings
                    myChannel.setLightColor(Color.GREEN);
                    myChannel.setVibrationPattern(vibrationScheme);
        
                    // Submit the notification channel object to the notification manager
                    getNotificationManager().createNotificationChannel(myChannel);
        
                }
            }
        
            /**
             * Build you notification with desired configurations
             *
             */
            public NotificationCompat.Builder getNotificationBuilder(String title, String body, Intent pendingIntent) {
        
                Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(
                        getApplicationContext().getResources(),
                        R.mipmap.ic_launcher);
        
                return new NotificationCompat.Builder(getApplicationContext(), MY_CHANNEL)
                        .setSmallIcon(R.drawable.ic_notification_icon)
                        .setLargeIcon(notificationLargeIconBitmap)
                        .setContentTitle(title)
                        .setContentText(body)
                        .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                        .setVibrate(vibrationScheme)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
                        .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(true);
            }
        
            public NotificationManager getNotificationManager() {
                if (mNotificationManager == null) {
                    mNotificationManager =
                            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                }
                return mNotificationManager;
            }
        
        }
        

        然后如下使用,

        NotificationHelper notificationHelper = new NotificationHelper(context);
        NotificationCompat.Builder builder = notificationHelper.getNotificationBuilder(title, message);
        notificationHelper.getNotificationManager().notify(notificationID, builder.build());
        

        【讨论】:

        • 不知何故我无法获得更新的构造函数,我的应用程序总是给我一个错误,说 NotificationCompat.Builder 只接受上下文作为参数,也不能接受字符串。
        • snag.gy/5kqVou.jpg 我仍然在为此苦苦挣扎,并且无能为力。也许是因为我在奥利奥出来之前就做了这个项目?
        • @PiotrOlech:检查您是否正在导入 android.support.v4.app.NotificationCompat
        • @PiotrOlech:我添加了一个例子,试试吧
        猜你喜欢
        • 2021-06-17
        • 1970-01-01
        • 2020-12-08
        • 1970-01-01
        • 2021-02-09
        • 1970-01-01
        • 1970-01-01
        • 2018-03-19
        • 1970-01-01
        相关资源
        最近更新 更多