[英]Android notification isn't showing up
I am attempting to show a small notification in my android app but I cannot visually see anything. 我试图在我的android应用中显示一条小通知,但我看不到任何东西。
MainActivity
class has a static field
named activity
which is is equal to this
. MainActivity
类具有一个名为activity
的static field
,它等于this
。 message.data
is just pulled from an object. message.data
只是从对象中提取的。 Here is what I am calling: 这就是我所说的:
sendNotification(MainActivity.activity.getApplicationContext(),
null,
message.data,
message.data,
1, true, true, true, 0);
And here is the meat: 这是肉:
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);
}
I can log messages so I know it is getting called and such- however, I am a beginner to android development and likely am missing a critical part. 我可以记录消息,所以我知道它会被调用,但是,我是android开发的初学者,很可能缺少关键部分。 I am running on an emulator android 2.2.
我在模拟器android 2.2上运行。 This is all in a plugin object that I have linked to cordova that is running in the background, if that matters at all.
这一切都在我链接到在后台运行的cordova的插件对象中(如果有的话)。
I created a new class for the intent: 我为此创建了一个新类:
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;
}
}
And I call it using this function 我用这个功能来称呼它
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 and David Wasser (In the comments) were correct in stating that a combination of using a correct Intent and correct context would solve this issue. ClassStacker和David Wasser(在评论中)指出使用正确的Intent和正确的上下文的组合可以解决此问题是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.