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 部分有点困惑。任何澄清我怀疑是不正确的代码将不胜感激。

4

5 回答 5

31

即使您的问题已解决,我也会发布我如何解决通知未显示的问题,也许它可能会帮助其他人阅读答案:

在我的通知大楼中,我错过了图标。一旦我添加了类似setSmallIcon(R.drawable.ic_launcher)通知的内容,就会显示出来。

于 2013-02-06T17:43:36.460 回答
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

于 2012-01-27T07:46:57.223 回答
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);
于 2012-01-27T06:18:57.600 回答
2

要尝试的另一件事是确保您的清单包含

<permission android:name="android.permission.STATUS_BAR_SERVICE" android:protectionLevel="signature" />

我的似乎也忽略了具有相同 NOTIFICATION_ID 的连续通知。

于 2012-05-03T01:44:22.407 回答
0

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

于 2016-04-24T00:15:57.523 回答