【问题标题】:Android - How to display the pushNotification even when the app is killed? [duplicate]Android - 即使应用程序被杀死,如何显示 pushNotification? [复制]
【发布时间】:2018-05-06 22:07:40
【问题描述】:

您好,我正在尝试使用 BroadcastReceiver 显示推送通知,当应用程序最小化时它可以正常工作,但是当应用程序关闭时,我无法显示推送通知。

我的问题是在应用关闭时显示推送通知的可能技术或解决方案是什么?

下面是使用广播接收器的通知代码

    public class Alarm extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String message=intent.getStringExtra("message");
        String title=intent.getStringExtra("title");
        String click_action=intent.getStringExtra("click_action");
      notification(context,message,title,click_action,intent);
    }

    private void notification(Context context, String message, String title, String click_action, Intent intent) {

        Toast.makeText(context, title + message + click_action, Toast.LENGTH_SHORT).show();
        if (click_action.equals("Time_LineActivity")) {
            intent = new Intent(context, Time_LineActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } else if (click_action.equals("MainActivity")) {
            intent = new Intent(context, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } else {
            intent = new Intent(context, MainActivity.class);
            intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
        }
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Notification.Builder notification = new Notification.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notification.build());

    }
}

下面是我的 FirebaseMessagingService 代码

public class MyFirebaseMessagingService extends  FirebaseMessagingService {
    private static final String TAG = "MyFirebaseMsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().get("title" +" "+"body" +" "+"click_action"));
        String title=remoteMessage.getData().get("title");
        String message=remoteMessage.getData().get("body");
        String click_action=remoteMessage.getData().get("click_action");
        sendNotification(title,message,click_action);
    }
    private void sendNotification(String title, String message, String click_action) {
        Intent broadcastedIntent=new Intent(this, Alarm.class);
        broadcastedIntent.putExtra("message", message);
        broadcastedIntent.putExtra("title", title);
        broadcastedIntent.putExtra("click_action", click_action);
        sendBroadcast(broadcastedIntent);
    }
}

提前谢谢..

【问题讨论】:

  • 您可以使用 Firebase Cloud Messagin (FCM) 系统在应用关闭时显示推送通知。这是一个tutorial
  • 已经实现,实际上我只从 FirebaseMessagingService 调用了这个广播接收器,但仍然无法正常工作
  • 即使在应用程序关闭时也应该显示推送通知。如果它不以这种方式工作,则可能与您的操作系统有关。可能是您在 MIUI 等限制性 rom 上运行应用程序。 Read this 了解更多。
  • 好吧,我的手机是华硕 zenfone 2 激光,上面有 android 6.0.1。有什么关系吗?

标签: android push-notification broadcastreceiver


【解决方案1】:

好的,感谢您的所有宝贵意见,我知道这个主题有几个主题,但对我没有任何帮助,这让我发疯了。

现在解决方案:

它是我手机中的移动管理器应用程序的老兄,它可以选择禁用某些应用程序的自动启动权限,我将其添加到自动启动列表中,现在它运行良好。我什至在其他智能手机中也看到了这一点,比如小米、OPPO、一加。这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多