安卓通知无法正常工作

10

我一整天都在试图让 ASyncTask 成功上传后通知我,但是我目前的代码没有任何错误,却无法在通知栏(或任何其他地方)显示通知。LogCat 中没有任何消息,通知栏中也没有任何通知出现。这是我的代码:

Notification mNotification = new Notification(icon, tickerText, when);

CharSequence contentTitle = "upload completed.";
CharSequence contentText = "upload completed.";

Intent notificationIntent = new Intent(context, CastrActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_NO_CREATE);
mNotification.contentIntent = contentIntent;
mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);

这是从 ASyncTask 的 onPostExecute() 方法调用的。说实话,我对 PendingIntent 部分有些困惑。非常感谢能够澄清我怀疑存在的错误代码。

5个回答

31

虽然你的问题已经解决了,但是我想分享一下我是如何解决通知不显示的问题的,希望对其他人有所帮助:

在我的通知构建中,我缺少了图标。当我添加了类似 setSmallIcon(R.drawable.ic_launcher) 的代码后,通知就能正常显示了。


是的,对我也起作用了。第一次使用通知。非常感谢! - Edison Santos
没错!非常感谢! - Alexandr
可以了!非常感谢。虽然如果开发者没有设置默认的启动器图标,Android应该只选择默认的启动器图标。 - Sohaib Shaheen
刚刚浪费了好几个小时来解决这个问题,就因为一个小小的图标。真是太感谢你了! - Miguel El Merendero
我有图标,但通知仍然无法工作,我浪费了很多时间,但当我将图标更改为测试时,它开始工作了,所以肯定是图标的问题。非常感谢你,伙计! - moDev

4

我已经创建了一个用于显示通知的类:

public class NotificationData {

    public static NotificationManager mNotificationManager;
    public static int SIMPLE_NOTFICATION_ID;
    private Context _context;

    public NotificationData(Context context) {
        _context = context;
    }

    public void clearNotification() {
        mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
    }

    public void SetNotification(int drawable, String msg, String action_string, Class cls) {
        mNotificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
        final Notification notifyDetails = new Notification(drawable, "Post Timer", System.currentTimeMillis());
        long[] vibrate = { 100, 100, 200, 300 };
        notifyDetails.vibrate = vibrate;
        notifyDetails.ledARGB = 0xff00ff00;
        notifyDetails.ledOnMS = 300;
        notifyDetails.ledOffMS = 1000;
     // notifyDetails.number=4;
        notifyDetails.defaults =Notification.DEFAULT_ALL;
        Context context = _context;
        CharSequence contentTitle = msg;
        CharSequence contentText = action_string;      
        Intent notifyIntent = new Intent(context,  cls);
     // Bundle bundle = new Bundle();
     // bundle.putBoolean(AppConfig.IS_NOTIFICATION, true);
        notifyIntent.putExtras(bundle);
        PendingIntent intent = PendingIntent.getActivity(_context, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
        notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
        mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);        
    }
}

使用此类的方法:

NotificationData notification; //create object
notification = new NotificationData(this);
notification.SetNotification(R.drawable.notification, "Notification Title", "Click to open", YourClassName.class);

添加权限 android.permission.VIBRATE


很抱歉,请问AppConfig是什么?我需要包含哪个库才能使用它?如果有的话,Eclipse似乎不知道,所以我必须将其添加到我的构建路径中。 - Carnivoris
Appconfig是一个类,而IS_NOTIFICATION是一个静态成员,你可以删除这一行。Bundle bundle=new Bundle(); bundle.putBoolean(AppConfig.IS_NOTIFICATION, true); notifyIntent.putExtras(bundle); - Munish Kapoor
不幸的是,我仍然没有收到通知。我是从ASyncTask类的onPostExecute()方法中调用它的。我通过LogCat中的消息确认ASyncTask已完成,但我没有收到通知发送到通知栏。 - Carnivoris
@MunishKapoor 当用户点击通知时,如何启动我的应用程序? - Qadir Hussain
@MunishKapoor 我遇到了这个问题:无法将 AppConfig 解析为变量。 - Qadir Hussain
显示剩余2条评论

2

试试这个:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

int icon = R.drawable.icon;        // icon from resources
CharSequence tickerText = "Any thing";              // ticker-text
long when = System.currentTimeMillis();         // notification   time
Context context21 = getApplicationContext();      // application   Context
CharSequence contentTitle = "Anything";  // expanded message title
CharSequence contentText = (CharSequence)  extras.get("message");     // expanded message text

Intent notificationIntent = new Intent(this, MainStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,   notificationIntent, 0);

// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
/*  long[] vibrate = { 0, 100, 200, 300 };
notification.vibrate = vibrate;
notification.ledARGB = Color.RED;
notification.ledOffMS = 300;
notification.ledOnMS = 300;*/
notification.setLatestEventInfo(context21, contentTitle, contentText, contentIntent);
mNotificationManager.notify(Constants.NOTIFICATION_ID, notification);

我遇到了与之前类似的问题。Intent notificationIntent = new Intent(this, CastrRecorder.class);这一行被Eclipse标记,唯一的解决方法是删除参数。此外,这是在扩展ASyncTask的类中调用的,getActivity()无法使用。 - Carnivoris

2
另一个可以尝试的方法是确保你的清单文件包含了该元素。
<permission android:name="android.permission.STATUS_BAR_SERVICE" android:protectionLevel="signature" />

我发现我的系统好像忽略了同一个通知ID的连续通知。


0
对我来说,这种情况一直在发生,但我不知道原因。问题是我设置的图标太大了,所以它给了我一些随机错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接