【问题标题】:Android show a notification not workingAndroid显示通知不起作用
【发布时间】:2014-10-13 17:48:47
【问题描述】:

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

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);
}

该方法在服务 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();
}

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

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

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

马库斯

【问题讨论】:

    标签: android android-intent android-notifications


    【解决方案1】:

    根据notification guide list of required fields,您必须通过setSmallIcon() 方法设置一个小图标。通常,此图标看起来像您的应用程序图标,尽管根据 anatomy of a notification 在透明背景上完全是白色的。

    【讨论】:

    • 非常感谢,这解决了。我不知道该图标是必需的。赞赏!
    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多