从Android中的服务发送通知

e-s*_*tis 101 service notifications android

我有一个服务正在运行,并希望发送通知.太糟糕了,通知对象需要一个Context,就像一个Activity,而不是一个Service.

你知道通过哪种方式吗?我试图Activity为每个通知创建一个,但它看起来很难看,我找不到一种方法来启动Activity没有任何通知View.

Jos*_*ger 105

无论ActivityService实际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);
Run Code Online (Sandbox Code Playgroud)

  • 将此更新到Notification.Builder apis会很高兴 (8认同)
  • 请记住,从服务中获取通知时会遇到很多问题.如果您遇到问题,请查看此http://groups.google.com/group/android-developers/browse_thread/thread/e95740e776982f89 (3认同)

tra*_*nte 75

从文档中可以看到这种类型的通知:

@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 */ }
Run Code Online (Sandbox Code Playgroud)

更好的方法
您可以发送这样的通知:

// 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); 
Run Code Online (Sandbox Code Playgroud)

最佳方式
以上代码需要最低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());
Run Code Online (Sandbox Code Playgroud)

  • 这应该是最好的答案,因为已接受的答案已被弃用 (4认同)
  • @MarcelKrivek似乎他或她"忘了"引用他们的来源.http://www.vogella.com/tutorials/AndroidNotifications/article.html (4认同)

王怡飞*_*王怡飞 8

@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);
}
Run Code Online (Sandbox Code Playgroud)