通知未显示

时间:2018-07-18 16:11:07

标签: android notifications broadcastreceiver android-notifications

我为我的项目创建了一个通知选项。它包含两个类:alarmreceiver(由AlarmManager调用)和一个创建通知的Notificationhelper类。

当我运行程序时,它没有显示通知而是崩溃了。有人请帮我解决这个问题。我在这里附加了代码

主要活动

    private void notification(String botime, String bodate){
                      Calendar cal=Calendar.getInstance();
                      cal.set(Calendar.HOUR_OF_DAY,21);
                      cal.set(Calendar.MINUTE,5);
                      cal.set(Calendar.SECOND,0);
                        AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
                        Intent intent=new Intent(getApplicationContext(),alarmreceiver.class);
                        PendingIntent pendingIntent=PendingIntent.getBroadcast(booking.this,1,intent,0);
                        //if(cal.before(Calendar.getInstance()))
                        //{
                            //cal.add(Calendar.DATE,1);
                        //}
                        alarmManager.setExact(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);  
}

AlarmReceiver

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class alarmreceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Notificationhelper notificationhelper=new Notificationhelper(context);
        NotificationCompat.Builder nb=notificationhelper.getchannelNotification("Remainder",
                "Remainder for your room booking");
        notificationhelper.getManager().notify(1,nb.build());
    }
}

Notificationhelper

package com.example.sarukesi.seminarbook;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;

public class Notificationhelper extends ContextWrapper {
    public static final String channelid="channelid";
    public static final String channelname = "channel";
    private NotificationManager mmanager;
    public Notificationhelper(Context base) {
        super(base);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            createchannels();
        }
    }

    private void createchannels() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel=new NotificationChannel(channelid,channelname, NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.enableLights(true);
            notificationChannel.enableVibration(true);
            notificationChannel.setLightColor(R.color.colorPrimary);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        }
    }
    public NotificationManager getManager(){
        if(mmanager==null){
            mmanager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mmanager;
    }
    public android.support.v4.app.NotificationCompat.Builder getchannelNotification(String title,String message){
        return new android.support.v4.app.NotificationCompat.Builder(getApplicationContext(),channelid)
                .setContentTitle(title)
                .setContentText(message);
    }
}

enter image description here

0 个答案:

没有答案