iT邦幫忙

0

Android Studio Notification出錯

package user.example.test;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.AlertDialog.*;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.view.*;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentTitle("標題")
                .setContentText("訊息內容")
                .setAutoCancel(true);
        NotificationChannel channel = new NotificationChannel("channel_1", "123", NotificationManager.IMPORTANCE_LOW);
        NotificationManager notifyManger = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notifyManger.createNotificationChannel(channel);
        notifyManger.notify(1, builder.build());
    }
}

new NotificationChannel會顯示紅色
https://ithelp.ithome.com.tw/upload/images/20210104/201296260SCuhKLjQt.png

2 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2021-01-05 09:22:38

1.我複製你的 source code,不會紅字
https://ithelp.ithome.com.tw/upload/images/20210105/20001787Ux21wWjzug.png

2.你的 code 寫法已經 deprecated 了,如果不是維護既有程式而是新寫的程式,建議參考官方教學官方範例 code

謝謝,我試試

我要發表回答

立即登入回答