簡體   English   中英

android studio通知不起作用

[英]android studio notification arent working

我是 android 新手,我希望用戶在達到最大值時收到通知。 這就是我寫代碼的方式

countUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(highestText == 0 && minimumText == 0){
                Toast.makeText(getApplicationContext(),"Please enter the target numbers!",Toast.LENGTH_SHORT).show();
            }else {

                if (highestText == minimumText) {
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, "number");
                    builder.setContentTitle("Maximum number ");
                    builder.setContentText("please be aware that you reached the maximum number");
                    builder.setSmallIcon(R.drawable.ic_launcher_background);
                    builder.setAutoCancel(true);
                    NotificationManagerCompat managerCompat = NotificationManagerCompat.from(MainActivity.this);
                    managerCompat.notify(1, builder.build());
                }

            }
            if(!(highestText == 0 && minimumText == 0)){
                count++;
                counter.setText("" + count);
            }
        }
    });

我不確定您沒有使用通知渠道 ID,但這可能是問題的原因。 我正在粘貼項目中的工作代碼片段,它應該可以正常工作,但如果它不起作用,請告訴我

public void showNotification(String Title, String Body){

    //  PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, splash_screen.class), 0);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "channel_id_01";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);

        // Configure the notification channel.
        notificationChannel.setDescription("Channel description");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        notificationChannel.enableVibration(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

    notificationBuilder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.notification_icon)
            //setticker is a text which is pronounce by accessibility service for differently abled people
            .setTicker("NEW NOTIFICATION")
           // .setAutoCancel(false)
            //        .setContentIntent(pi)
            .setPriority(Notification.PRIORITY_MAX)
            .setContentTitle(Title)
            .setContentText(Body)
            .setContentInfo("Info");

    notificationManager.notify(/*notification id*/1, notificationBuilder.build());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM