Android通知未显示

时间:2013-05-05 09:03:58

标签: java android notifications

我试图在我的Android应用中显示一个小通知,但我无法直观地看到任何内容。 MainActivity类的static field名为activity,等于thismessage.data只是从一个物体中拉出来的。这就是我所说的:

sendNotification(MainActivity.activity.getApplicationContext(), 
                 null, 
                 message.data, 
                 message.data, 
                 1, true, true, true, 0);

这是肉:

public static void sendNotification(Context caller, Class<?> activityToLaunch, String title, String msg, int numberOfEvents,boolean sound, boolean flashLed, boolean vibrate,int iconID) {
    NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notify = new Notification(R.drawable.ic_launcher,"Notification",System.currentTimeMillis());

    notify.icon         = iconID;
    notify.tickerText   = title;
    notify.when         = System.currentTimeMillis();
    notify.number       = numberOfEvents;
    notify.flags        |= Notification.FLAG_AUTO_CANCEL;

    if (sound)   notify.defaults    |= Notification.DEFAULT_SOUND;
    if (vibrate) notify.vibrate     = new long[] {100, 200, 300};
    if (flashLed) {
        notify.flags    |= Notification.FLAG_SHOW_LIGHTS;
        notify.ledARGB  = Color.CYAN;
        notify.ledOnMS  = 500;
        notify.ledOffMS = 500;
    }

    int NOTIFICATION_ID = 1232;

    Intent notificationIntent = new Intent();
    PendingIntent contentIntent = PendingIntent.getActivity(caller, 0, notificationIntent, 0);
    notify.setLatestEventInfo(caller, title, msg, contentIntent);
    notifier.notify(NOTIFICATION_ID, notify);
}

我可以记录消息,所以我知道它正在被调用等等 - 但是,我是android开发的初学者,可能错过了一个关键部分。我在模拟器android 2.2上运行。这是一个插件对象,我已经链接到后台运行的cordova,如果这很重要的话。

1 个答案:

答案 0 :(得分:0)

我为意图创建了一个新类:

public class StatusNotificationIntent {
    public static Notification buildNotification( Context context, CharSequence tag, CharSequence contentTitle, CharSequence contentText) {
        int icon = R.drawable.notification;
        long when = System.currentTimeMillis();
        Notification noti = new Notification(icon, contentTitle, when);
        noti.flags |= flag;

        PackageManager pm = context.getPackageManager();
        Intent notificationIntent = pm.getLaunchIntentForPackage(context.getPackageName());
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notificationIntent.putExtra("notificationTag", tag);

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        noti.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        return noti;
    }
}

我用这个函数调用它

public void showNotification( CharSequence tag, CharSequence contentTitle, CharSequence contentText, int flag) {
    String ns = Context.NOTIFICATION_SERVICE;
    context = cordova.getActivity().getApplicationContext();
    mNotificationManager = (NotificationManager) context.getSystemService(ns);

    Notification noti = StatusNotificationIntent.buildNotification(context, tag, contentTitle, contentText);
    mNotificationManager.notify(tag.hashCode(), noti);
}

ClassStacker和David Wasser(在评论中)说明使用正确的Intent和正确的上下文组合可以解决这个问题是正确的。