19

即使我的应用程序已关闭,我也试图在 Android 通知栏中显示通知。

我试过搜索,但我没有找到帮助。

这方面的一个例子是新闻应用程序。即使手机屏幕关闭或新闻应用程序关闭,它仍然可以发送最近新闻的通知并将其显示在通知栏中。

我该如何在自己的应用程序中执行此操作?

4

3 回答 3

18

您必须构建一个服务来处理您的新闻并在它知道是新新闻时显示通知(服务文档)。即使您的应用程序已关闭,该服务也会在后台运行。启动阶段完成后,您需要一个BroadcastReciever在后台运行服务。(开机后启动服务)。

该服务将构建您的通知并通过NotificationManager发送它们。

编辑:这篇文章可能适合您的需要

于 2013-09-15T13:01:30.437 回答
3

所选答案仍然正确,但仅适用于运行 Android 7 及以下版本的设备。从 Android 8+ 开始,当您的应用空闲/关闭时,您不能再让服务在后台运行。
因此,现在取决于您如何设置来自 GCM/FCM 服务器的通知。确保将其设置为最高优先级。如果您的应用程序在后台或只是未处于活动状态并且您只发送通知数据,系统会处理您的通知并将其发送到通知托盘。

于 2018-12-13T04:16:39.123 回答
1

我用这个答案编写了一个服务,作为一个例子,你需要ShowNotificationIntentService.startActionShow(getApplicationContext())在你的一个活动中调用:

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
public class ShowNotificationIntentService extends IntentService {
    private static final String ACTION_SHOW_NOTIFICATION = "my.app.service.action.show";
    private static final String ACTION_HIDE_NOTIFICATION = "my.app.service.action.hide";


    public ShowNotificationIntentService() {
        super("ShowNotificationIntentService");
    }

    public static void startActionShow(Context context) {
        Intent intent = new Intent(context, ShowNotificationIntentService.class);
        intent.setAction(ACTION_SHOW_NOTIFICATION);
        context.startService(intent);
    }

    public static void startActionHide(Context context) {
        Intent intent = new Intent(context, ShowNotificationIntentService.class);
        intent.setAction(ACTION_HIDE_NOTIFICATION);
        context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_SHOW_NOTIFICATION.equals(action)) {
                handleActionShow();
            } else if (ACTION_HIDE_NOTIFICATION.equals(action)) {
                handleActionHide();
            }
        }
    }

    private void handleActionShow() {
        showStatusBarIcon(ShowNotificationIntentService.this);
    }

    private void handleActionHide() {
        hideStatusBarIcon(ShowNotificationIntentService.this);
    }

    public static void showStatusBarIcon(Context ctx) {
        Context context = ctx;
        NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
                .setContentTitle(ctx.getString(R.string.notification_message))
                .setSmallIcon(R.drawable.ic_notification_icon)
                .setOngoing(true);
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(context, STATUS_ICON_REQUEST_CODE, intent, 0);
        builder.setContentIntent(pIntent);
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notif = builder.build();
        notif.flags |= Notification.FLAG_ONGOING_EVENT;
        mNotificationManager.notify(STATUS_ICON_REQUEST_CODE, notif);
    }
}
于 2016-08-02T18:09:32.897 回答