Android - 即使应用程序被杀,如何显示pushNotification?

时间:2017-11-23 03:26:14

标签: android push-notification broadcastreceiver

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

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

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

    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);
    }
}

提前致谢..

1 个答案:

答案 0 :(得分:0)

好的,感谢你所有宝贵的评论,我知道这个主题有几个主题,但对我没什么用,这让我发疯了。

现在是解决方案:

这是我手机中的移动经理应用程序的老兄,它可以选择禁用某些应用程序的自动启动权限,我添加到自动启动列表中,现在它运行良好。我甚至在其他智能手机中看到过像小米,Oppo,One Plus这样的人。这对我有用。