通知不出现

时间:2019-05-12 16:52:30

标签: android push-notification notifications android-alarms android-push-notification

我正在尝试制作一个Android提醒应用。我要在特定时间显示通知的地方。

我已经在youtube上观看了一个视频,并完美地跟随了它。但是,我的代码无法正常工作。另外,android studio不会显示任何错误。

问题在于通知根本没有显示。任何帮助,将不胜感激。在此先感谢:)

这是MainActivity:

package com.saipriyank.remind;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private int nid = 1;

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

        findViewById(R.id.bt_set).setOnClickListener(this);
        findViewById(R.id.bt_cancel).setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        EditText et = findViewById(R.id.et);
        TimePicker tp = findViewById(R.id.tp);

        Intent i = new Intent(MainActivity.this,AlarmReceiver.class);
        i.putExtra("notificationId",nid);
        i.putExtra("Todo", et.getText().toString());

        PendingIntent alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0,i,PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);

        switch (view.getId()){
            case R.id.bt_set:
            int hour = tp.getCurrentHour();
            int minute = tp.getCurrentMinute();


                Calendar startTime = Calendar.getInstance();
                startTime.set(Calendar.HOUR_OF_DAY,hour);
                startTime.set(Calendar.MINUTE,minute);
                startTime.set(Calendar.SECOND,0);

                long alarmStartTime = startTime.getTimeInMillis();

                alarm.set(AlarmManager.RTC_WAKEUP,alarmStartTime,alarmIntent);
                Toast.makeText(this,"Reminder has been set.",Toast.LENGTH_SHORT).show();
                break;

            case R.id.bt_cancel:
                alarm.cancel(alarmIntent);
                Toast.makeText(this,"Reminder has been cancelled.",Toast.LENGTH_SHORT).show();
                break;

        }
    }
}

这是AlarmReceiver.java

package com.saipriyank.remind;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        int nid = intent.getIntExtra("notificationID",0);
        String message = intent.getStringExtra("Todo");

        Intent mainIntent = new Intent(context,MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context,0,mainIntent,0);

        NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(context);

        builder.setSmallIcon(R.drawable.icon)
                .setContentTitle("Reminder")
                .setContentText(message)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentIntent(contentIntent)
                .setPriority(Notification.PRIORITY_MAX)
                .setDefaults(Notification.DEFAULT_ALL);

        nm.notify(nid,builder.build());


    }
}

1 个答案:

答案 0 :(得分:0)

您需要为方法getSystemService提供另一个参数,该参数是一个通道。 像这样定义它,然后将其传递给方法

公共静态字符串CHANNEL_ID =“ MAIN_CHANNEL”;

将此行替换为您的

NotificationManager nm =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE,CHANNEL_ID);