提问人:Bite code 提问时间:7/30/2009 最后编辑:fawaadBite code 更新时间:3/9/2018 访问量:158550
在 Android 中从服务发送通知
Sending a notification from a service in Android
问:
我正在运行一项服务,并希望发送通知。太糟糕了,通知对象需要一个 ,就像 一样,而不是 .Context
Activity
Service
你知道有什么方法可以通过吗?我试图为每个通知创建一个,但它看起来很丑陋,我找不到一种方法来启动一个没有任何.Activity
Activity
View
答:
113赞
Josef Pfleger
7/31/2009
#1
两者实际上都是上下文
,因此您可以简单地将其用作 .Activity
Service
extend
this
Context
Service
NotificationManager notificationManager =
(NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
Notification notification = new Notification(/* your notification */);
PendingIntent pendingIntent = /* your intent */;
notification.setLatestEventInfo(this, /* your content */, pendingIntent);
notificationManager.notify(/* id */, notification);
评论
4赞
Karussell
4/3/2011
请记住,从服务发送通知时会遇到很多问题。如果你有问题,那就看看这个 groups.google.com/group/android-developers/browse_thread/thread/......
1赞
Kairi San
1/29/2016
如何使用 Notification.Builder 执行此操作?因为 setLatestEventInfo 已被弃用。
80赞
trante
10/28/2013
#2
从文档中可以看出,这种类型的通知已弃用:
@java.lang.Deprecated
public Notification(int icon, java.lang.CharSequence tickerText, long when) { /* compiled code */ }
public Notification(android.os.Parcel parcel) { /* compiled code */ }
@java.lang.Deprecated
public void setLatestEventInfo(android.content.Context context, java.lang.CharSequence contentTitle, java.lang.CharSequence contentText, android.app.PendingIntent contentIntent) { /* compiled code */ }
更好的方法
您可以发送如下通知:
// prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new Notification.Builder(this)
.setContentTitle("New mail from " + "[email protected]")
.setContentText("Subject")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.setAutoCancel(true)
.addAction(R.drawable.icon, "Call", pIntent)
.addAction(R.drawable.icon, "More", pIntent)
.addAction(R.drawable.icon, "And more", pIntent).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
最佳方式
上述代码至少需要 API 级别 11 (Android 3.0)。
如果最低 API 级别低于 11,则应使用支持库的 NotificationCompat 类,如下所示。
因此,如果您的最低目标 API 级别是 4+ (Android 1.6+),请使用以下命令:
import android.support.v4.app.NotificationCompat;
-------------
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.mylogo)
.setContentTitle("My Notification Title")
.setContentText("Something interesting happened");
int NOTIFICATION_ID = 12345;
Intent targetIntent = new Intent(this, MyFavoriteActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());
评论
4赞
Marcel Krivek
9/9/2015
这应该是最好的答案,因为已接受的答案已被弃用
4赞
StarWind0
5/23/2016
@MarcelKrivek 似乎他或她“忘记”引用他们的来源。vogella.com/tutorials/AndroidNotifications/article.html
0赞
user3690202
12/17/2017
什么是“NotificationReceiver”?
0赞
George Theodorakis
5/21/2018
“NotificationReceiver”是将由通知打开的活动。检查 @StarWind0 提供的链接。
0赞
Devil's Dream
7/21/2018
NotificationCompat.Builder 已弃用。它现在不再是最佳答案
1赞
Martin Pfeffer
10/2/2014
#3
好吧,我不确定我的解决方案是否是最佳实践。使用 my 代码如下所示:NotificationBuilder
private void showNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
清单:
<activity
android:name=".MainActivity"
android:launchMode="singleInstance"
</activity>
这里是服务:
<service
android:name=".services.ProtectionService"
android:launchMode="singleTask">
</service>
我不知道是否真的有 at,但这在我的应用程序中正常工作......singleTask
Service
评论
0赞
Viswanath Lekshmanan
5/9/2015
这里面的建设者是什么?
7赞
David Wang
9/28/2015
#4
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void PushNotification()
{
NotificationManager nm = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,0);
//set
builder.setContentIntent(contentIntent);
builder.setSmallIcon(R.drawable.cal_icon);
builder.setContentText("Contents");
builder.setContentTitle("title");
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
nm.notify((int)System.currentTimeMillis(),notification);
}
评论
0赞
Duna
11/20/2018
主题问题来自 SERVICE 而不是 Activity
-3赞
kurdist android
4/18/2016
#5
如果这些方法都不起作用,请尝试 ,而不是 或 。getBaseContext()
context
this
评论
2赞
Rahat Zaman
11/28/2017
您不应使用这些方案。getBaseContext()
评论