Lỗi khi tạo một notification trong android

Em đang làm cái hẹn giờ xuất hiện thông báo.Nhưng mà ở cái class Alarm này sao em tạo cái notification nó lại báo lỗi ở chỗ this và getSystemService ở cả hai hàm vậy ạ?Còn khi em tạo notification ở cái class MainAcitivy thì lại không lỗi lầm gì

package com.example.reminder;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.core.app.NotificationCompat;

public class Alarm extends BroadcastReceiver {
    public static final  String CHANNEL_1_ID = "channel1";
    public static final  String CHANNEL_2_ID = "channel2";
    @Override
    public void onReceive(Context context, Intent intent) {
        createNotificationChannels();
        createNotification();

    }
    private void createNotification(){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_1_ID)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle("Hello")
                .setContentText("Day la thong bao")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }
    private void createNotificationChannels()  {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel1 = new NotificationChannel(
                    CHANNEL_1_ID,
                    "Channel 1",
                    NotificationManager.IMPORTANCE_HIGH
            );
            channel1.setDescription("This is channel 1");

            NotificationChannel channel2 = new NotificationChannel(
                    CHANNEL_2_ID,
                    "Channel 2",
                    NotificationManager.IMPORTANCE_LOW
            );
            channel1.setDescription("This is channel 2");
            NotificationManager manager = this.getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel1);
            manager.createNotificationChannel(channel2);
        }
    }
}

Đây là lỗi “chưa đỗ ông nghè đã đe hàng tổng” ấy bạn, cậu đơn giản search xem cái method getSystemService nó trả về cái gì là sẽ hiểu tại sao call ở đây lỗi mà trong MainActivity ko lỗi.

5 Likes

getSystemService thuộc 1 ngữ cảnh cụ thể (Context) vì vậy bạn phải dùng context.getSystemService

Còn khi em tạo notification ở cái class MainAcitivy thì lại không lỗi lầm gì

-> this trong activity là 1 instance của Conext vì vậy có thể dùng this.getSystemService được

sao em tạo cái notification nó lại báo lỗi ở chỗ this và getSystemService ở cả hai hàm vậy ạ

-> còn this ở đây không phải instance của Context vì vậy không gọi được this.getSystemService
-> solution: dùng cái context bạn nhận được tại hàm onReceive, gọi getSystemService từ cái ngữ cảnh (context) đó

5 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?