繁体   English   中英

状态栏通知未显示

[英]Status bar Notification not showing up

我有一个简单的通知设置,应该在时间到时在状态栏上显示通知(基本上是剩余时间)。 我这样设定时间:

Calendar cal = Calendar.getInstance();
        cal.set(2016, 0, 30, 10, 56, 00);
        alarmReceiver.setCredentials(NAME,PODCAST);
        setAlarm(cal);

setAlarm方法在此处给出:

private void setAlarm(Calendar target){

    Toast.makeText(c, "Reminder set", Toast.LENGTH_LONG).show();
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), req1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, target.getTimeInMillis(), pendingIntent);
}

这将调用AlarmReciever类,该类是:

  public class AlarmReceiver extends BroadcastReceiver {
static int notifyId=1;
String name="";
String disc="";
NotificationCompat.Builder notification;
PendingIntent pIntent;
NotificationManager manager;
Intent resultIntent;
TaskStackBuilder stackBuilder;
public void setCredentials(String n,String d){
    name=n;
    disc=d;
}

@Override
public void onReceive(Context context,Intent intent) {
    //Toast.makeText(context,"Alarm has been set",Toast.LENGTH_SHORT).show();
    Log.d("Midhun","onReceiver receiver...");
    notification = new NotificationCompat.Builder(context);
    //set title for NOTIFICATION
    notification.setContentTitle(name);
    //message in notification
    notification.setContentText(disc);
    //Alert shown when notification recieved
    notification.setTicker("INVENTO 16");
    notification.setSmallIcon(R.drawable.ic_notify);
    stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    resultIntent = new Intent(context,MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    pIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setContentIntent(pIntent);
    manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(10,notification.build());
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(context, notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

问题在于通知未显示。

我尝试了以下方法:

1.我从AlarmReceiver类中打印了一条语句,看是否调用了该方法。消息在正确的时间显示在控制台中(我在该行中设置的时间: cal.set(2016, 0, 30, 08, 56, 00); )即。 @ 2015年1月30日8:56。

2.也调用setAlarm时显示的Toast。

所以基本上问题是我的AlarmReciever类。但是我无法弄清楚它是什么。

好吧,当我删除语句stackBuilder.addParentStack(MainActivity.class);时,它开始为我工作stackBuilder.addParentStack(MainActivity.class);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM