android - 在 android 服务中使用 NotificationManager

标签 android service notificationmanager

我有一个服务在后台运行,如果有新数据我想通知用户,我使用了一个通知管理器,通知出现了,但是当点击它时它没有做任何事情(它是应该显示一个 Activity 我是 android 新手,这是我的代码

NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = newNotification(R.drawable.shoppingcarticon, "Nouvelle notification de ShopAlert", System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;

Intent intent = new Intent(this,GooglemapActivity.class);
PendingIntent activity = PendingIntent.getService(this, 0, intent, 0);

notification.setLatestEventInfo(this, "This is the title", "This is the text", activity);
notification.number += 1;
notificationmanager.notify(0, notification);

我们将不胜感激。

最佳答案

您可以使用此代码。将 Main.Class 更改为您的 Activity 类,以及您需要的标题和内容文本。

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


int icon = R.drawable.ic_action_search;
CharSequence tickerText = "Pet Parrot";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Context context = getApplicationContext();
CharSequence contentTitle = "Hungry!";
CharSequence contentText = "your parrot food meter is: ";
Intent notificationIntent = new Intent(context, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

mNotificationManager.notify(1, notification); // Log.d("test", "Saving Data to File from Service.");

关于android - 在 android 服务中使用 NotificationManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11599406/

相关文章:

当应用程序被滑动时,Android 在服务中维护变量值

使用 Flask 的 Python Web API

android - 如何使用声音和振动通知?

Android 通知管理器在屏幕关闭时不起作用

android - 以编程方式选中/取消选中 ListView 内的复选框?

android - org.json.JSONException : Value of type java. lang.String 无法转换为 JSONObject

AngularJS Factory.XXX 不是一个函数

java - 使用 OpenCV 从图像序列中获取中值图片

Android Gradle 构建错误 :Ticks in transparent frame must be black or red

android - 通知未出现在 notify() 上