2

我试图在我的 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的插件对象中,如果这很重要的话。

4

1 回答 1

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 和正确的上下文可以解决这个问题。

于 2013-05-07T21:35:12.793 回答