Android 显示通知不起作用

Mar*_*cus 5 android android-intent android-notifications

目前我有一个播放音乐的服务。我想在服务开始播放音乐时显示通知。这就是我所做的:

public void setUpNotification() {
    /*
     * Set up the notification for the started service
     */

    Notification.Builder notifyBuilder = new Notification.Builder(this);
    notifyBuilder.setTicker("ticker");
    notifyBuilder.setOngoing(true);
    notifyBuilder.setContentTitle("title");
    notifyBuilder.setContentInfo("content info");

    // set up an Intent if the user clicks the notification
    int NOTIFICATION_ID = 1;
    Intent notifyIntent = new Intent(this, MainActivity.class);

    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pi = PendingIntent.getActivity(this, 0, notifyIntent,
            NOTIFICATION_ID);

    notifyBuilder.setContentIntent(pi);
    Notification notification = notifyBuilder.build();
    // start ongoing
    startForeground(NOTIFICATION_ID, notification);
}
Run Code Online (Sandbox Code Playgroud)

该方法在服务 playMusic() 中被调用,如下所示:

public void playMusic(String songPath) {
    if(mPlayer == null || !mPlayer.isPlaying()){
        try {
            mPlayer = MediaPlayer.create(getBaseContext(), Uri.parse(songPath));
            mPlayer.start();
            this.songPath = songPath;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }else{
        mPlayer.stop();
        mPlayer.release();
        mPlayer = MediaPlayer
                .create(getBaseContext(), Uri.parse(songPath));
        mPlayer.start();
        this.songPath = songPath;
    }
    setUpNotification();
}
Run Code Online (Sandbox Code Playgroud)

问题是未显示通知待处理意图、内容标题和内容信息。该通知如下所示: 截屏

正如您所看到的,标题和内容信息不会显示。此外,当我单击通知时,它不会触发我的待处理意图。

我在这里做错了什么?任何帮助是极大的赞赏。

马库斯