如何关闭推送通知,同时关闭应用程序(甚至不在后台)

时间:2017-11-06 12:06:32

标签: android push-notification firebase-cloud-messaging android-pendingintent

我正在研究android中的firebase推送通知,我已经成功实现了,一切运行良好,我正在推送按钮上的特定活动,它工作但只有当我的应用程序在后台运行时,当应用程序关闭时(不在后台)然后点击推送从第一个活动(主要活动)启动应用程序。我搜索了很多链接没有运气,所以任何伙伴都可以帮我解决,我在这里发布我的代码。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";
    public static String PUSH_NOTIFICATION = "PUSH_NOTIFICATION";
    String store_id, img_url;
    String store_name;
    String headline;
    String store_img;
    int size = 200;
    String subtext;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        size = (int) Math.ceil(Math.sqrt(300 * 200));
        Message message = new Message();
        message.obj = remoteMessage;
        handler.sendMessage(message);
        Intent pushNotification = new Intent(this.PUSH_NOTIFICATION);
        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
// TODO(developer): Handle FCM messages here.
            Log.d(TAG, "=====push msg------by jigar--" + msg.toString());
            final RemoteMessage remoteMessage = (RemoteMessage) msg.obj;
            if (remoteMessage.getData().size() > 0) {
                Log.d(TAG, "Message data payload: " + remoteMessage.getData());
                final JSONObject jsonObject;
                try {
                    jsonObject = new JSONObject(remoteMessage.getData().toString());
                    Log.d(TAG, "tag: " + jsonObject.getString("store_id"));
                    Log.d(TAG, "message: " + jsonObject.getString("img_url"));
                    store_id = jsonObject.getString("store_id");
                    img_url = jsonObject.getString("img_url");
                    headline = jsonObject.getString("headline");
                    store_name = jsonObject.getString("store_name");
                    store_img = jsonObject.getString("store_image");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

                    Glide.with(getApplicationContext())
                            .load(img_url)
                            .asBitmap()
                            .into(new SimpleTarget<Bitmap>() {
                                @Override
                                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                                    resource.compress(Bitmap.CompressFormat.PNG, 100, out);
                                    Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

                                    sendNotification(remoteMessage.getNotification().getBody(), decoded, store_id, remoteMessage.getNotification().getTitle());
                                }
                            });
                } else {
                    sendNotification("", null, store_id, "");
                }
            }
        }
    };

    private void sendNotification(String messageBody, Bitmap bitmap, String storeId, String subtext) {
        Intent intent = null;
        intent = new Intent(this, ProductDescriptionActivity.class);
        intent.putExtra("storeid",store_id);
        Const.STORE_ID = store_id;
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT|PendingIntent.FLAG_UPDATE_CURRENT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            Log.e("===BITMAP ", bitmap + "");
            NotificationCompat.BigPictureStyle notyStyle = new NotificationCompat.BigPictureStyle();
            notyStyle.bigPicture(bitmap);
            mBuilder.setSmallIcon(R.drawable.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                            R.drawable.ic_launcher))
                    .setContentTitle(headline).setColor(getResources().getColor(R.color.orange))
                    .setContentText(store_name + "\n" + messageBody).setContentInfo(subtext)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent)
                    .setStyle(notyStyle);
        } else {
            mBuilder.setSmallIcon(R.drawable.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                            R.drawable.ic_launcher))
                    .setContentTitle(headline)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
        }
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Random r = new Random();
        notificationManager.notify(r.nextInt(80 - 65) + 65, mBuilder.build());
        PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
        wl.acquire(6000);
    }
}

2 个答案:

答案 0 :(得分:0)

请在AndroidManifest.xml文件的MainActivity标记中添加以下属性。

android:launchMode="singleTask"

答案 1 :(得分:0)

使用以下代码,我可以打开我的MainActivity。

NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    Intent notificationIntent = new Intent(context, MainActivity.class);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);

    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);