ANDROID显示一个通知无法正常工作 [英] Android show a notification not working

查看:154
本文介绍了ANDROID显示一个通知无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个播放音乐的服务。我想说明的通知时,该服务开始播放音乐。这是我做的:

Currently I have a service that plays music. I want to show a notification when the service start to play music. This is what I do:

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()是这样的:

The method gets called in the service playMusic() like this:

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

的问题是,待处理的意图,内容的标题和内容信息通知不获取显示。通知如下:

The problem is that the notifications pending intent, content title and content info does not get displayed. The notification looks like this:

正如你所看到的,标题和内容的信息不会被显示出来。此外,当我点击该通知不火过我挂起的意图。

And as you can see, the title and content info doesn't get displayed. Also, when I click the notification it does not fire off my pending intent.

我在做什么错在这里?任何帮助是极大的AP preciated。

What am I doing wrong here? Any help is greatly appreciated.

马库斯

推荐答案

占的必填字段通知指导列表,您必须设置通过<一个小图标href="http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setSmallIcon(int)"相对=nofollow> setSmallIcon()方法。一般情况下,这个图标看起来像你的应用程序图标,但完全白了占的的通知肖像指南

Per the notification guide list of required fields, you must set a small icon via the setSmallIcon() method. Generally, this icon looks like your application icon, although entirely as white over a transparent background per the notification iconography guide.

这篇关于ANDROID显示一个通知无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆