一段时间后如果需要,通知不会显示。但只在现场工作

notification not showing if needed after some time. but working on the spot only

我的应用程序中有 3 个按钮

第一个按钮在单击按钮时显示通知。

第二个按钮正在取消显示的通知。

第三个按钮必须在 5 秒后显示通知。

第一个和第二个按钮有效,但第三个按钮无效。它在编译中没有显示任何错误。 谁能告诉我这段代码有什么问题吗?

public void setAlarm(View view) {
        Long alertTime=new GregorianCalendar().getTimeInMillis()+5000;
        Intent alertIntent=new Intent(this,AlertReceiver.class);
        AlarmManager alarmManager=(AlarmManager)
                getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime,
                 PendingIntent.getBroadcast(this,1, alertIntent,
                         PendingIntent.FLAG_UPDATE_CURRENT));
    }

这是我的完整 Activity 主要 Class

package com.test.notification2;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.GregorianCalendar;

public class MainActivity extends AppCompatActivity {

    Button showNotificationBut, stopNotificationBut,alertButton;
    NotificationManager notificationManager;

    boolean isNotificActive=false;

    int notifID=33;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        showNotificationBut=(Button)findViewById(R.id.showNotificationBut);
        stopNotificationBut=(Button)findViewById(R.id.stopNotificationBut);
        alertButton=(Button)findViewById(R.id.alertButton);


    }


    public void showNotification(View view) {
        NotificationCompat.Builder notificBuilder= new
                NotificationCompat.Builder(this)
                .setContentTitle("Message")
                .setContentText("New Message")
                .setTicker("Alert New Message")
                .setSmallIcon(R.drawable.my_image);

        Intent moreInfoIntent=new Intent(this,MoreInfoNotification.class);

        TaskStackBuilder taskStackBuilder=TaskStackBuilder.create(this);
        taskStackBuilder.addParentStack(MoreInfoNotification.class);
        taskStackBuilder.addNextIntent(moreInfoIntent);
        PendingIntent pendingIntent=taskStackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notificBuilder.setContentIntent(pendingIntent);

        notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(notifID, notificBuilder.build());
        isNotificActive=true;


    }

    public void stopNotification(View view) {
        if(isNotificActive){
            notificationManager.cancel(notifID);
        }
    }

    public void setAlarm(View view) {
        Long alertTime=new GregorianCalendar().getTimeInMillis()+5000;
        String s = String.valueOf(alertTime);
        Toast.makeText(this,s,Toast.LENGTH_SHORT).show();

        Intent alertIntent=new Intent(this,AlertReceiver.class);
        AlarmManager alarmManager=(AlarmManager)
                getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime,
                 PendingIntent.getBroadcast(this,1, alertIntent,
                         PendingIntent.FLAG_UPDATE_CURRENT));
    }
}

这里是 AlertReceiver Class:

package com.test.notification2;

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

/**
 * Created by waqas on 13/05/2016.
 */
public class AlertReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
       createNotification(context,"Times up","5 seconds has passed",
               "Alert");
    }
    public void createNotification(Context context,String msg,String msgText, String  msgAlert){
        PendingIntent notificIntent=PendingIntent.getActivity(context, 0,
                new Intent(context,MainActivity.class),0);
        NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.my_image)
                .setContentTitle(msg)
                .setTicker(msgAlert)
                .setContentText(msgText);
        mBuilder.setContentIntent(notificIntent);
        mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager=
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1,mBuilder.build());

    }
}

这段代码工作正常。 您是否在清单中注册了该服务??

        <receiver android:name=".AlertReceiver">
    </receiver>