我正在运行服务,并希望发送通知。太糟糕了,通知对象需要Context
,例如Activity
,而不是Service
。
你知道通过什么方式吗?我尝试为每个通知创建一个Activity
,但它似乎很难看,我找不到一种方法来启动Activity
而没有任何View
。
答案 0 :(得分:105)
Activity
和Service
实际上extend
Context
,因此您可以在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);
答案 1 :(得分:74)
从文档中可以看到这种类型的通知:
@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 " + "test@gmail.com")
.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,那么您应该像这样使用support library的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());
答案 2 :(得分:7)
<div class="input-group">
<input type="text" class="form-control" datepicker-popup="{{format}}" data-ng-model="mv.dateReviewed" is-open="statusDateReviewed.opened" ng-required="true" close-text="Close" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="openDateReviewed($event)"><i class="glyphicon glyphicon-calendar"></i> </button>
</span>
</div>
答案 3 :(得分:1)
好吧,我不确定我的解决方案是否是最佳做法。使用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>
我不知道singleTask
是否确实存在Service
,但这在我的应用程序中正常运行...
答案 4 :(得分:-2)
如果这些都无效,请尝试getBaseContext()
,而不是context
或this
。