Suppressing notification from package com.example.mynotification by user request.

在學Android多媒體Notification時,怎麼修改代碼,手機端就是不彈通知,翻看Log發現如標題錯誤,經在網上查資料,發現是手機設置問題,打開手機設置,通知與狀態欄,通知管理,找到對應的應用,允許該應用談通知即可。
代碼如下:

    private void createNotification() {
        Intent intent = new Intent(this, NotifictionActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("1", "name", NotificationManager.IMPORTANCE_HIGH);
            mNotificationManager.createNotificationChannel(channel);
            Notification notification = new NotificationCompat.Builder(this, "1")
                    .setContentTitle("我的通知")
                    .setContentText("我通知的內容")
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setAutoCancel(true)
                    .build();
            mNotificationManager.notify(1, notification);
        } else {
            Notification notification = new Notification.Builder(this)
                    .setContentTitle("我的通知")
                    .setContentText("我通知的內容")
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setAutoCancel(true).build();
            mNotificationManager.notify(1, notification);
        }
    }

參考鏈接:link

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章