当用户单击通知时,当应用程序处于前台时它不会启动特定活动

时间:2019-04-14 08:15:38

标签: android push-notification

当用户单击通知时,它不会启动特定活动。当用户杀死应用程序然后重新启动时,它会正常运行。当应用程序前台用户中的用户单击通知但未打开特定活动时,当用户再次发送通知时。

案例

A,当用户最初启动应用程序并收到通知时,然后用户单击某个特定活动即将开始的通知。当我杀死该应用程序并再次重新启动通知时,请单击通知,然后打开特定活动。但是现在我的应用程序处于前台/后台通知中,请点击该通知,以通知特定活动未打开的时间。

我的活动是一项基本活动,在我的活动中,我已经定义了多个条件

  • 关于我们的通知时,然后重定向到关于我们的片段
  • 出现聊天消息通知时,然后重定向到聊天 片段

代码

Notiifcationmessage service class TvamFirebaseMessagingService extends FirebaseMessagingService 
{

    private static final String TAG = "MyFirebaseMsgService";
    String CHANNEL_ID = "my_channel_01";// The id of the channel.
    private static PowerManager.WakeLock wakeLock;
    private Intent notificationIntent;
    private PendingIntent contentIntent;
    private String urlstring="";
    private String message;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.e("push", "data: " + remoteMessage.getFrom().toString());

        if (remoteMessage.getData().size() > 0) {
            acquire(getApplicationContext());
            release();
            Map<String, String> data = remoteMessage.getData();
            String notificationMsg = "",messageType="";


            try {
                JSONObject jsonObject = new     JSONObject(data.get("message"));
                notificationMsg = jsonObject.get("message").toString();
//                sendNotification(notificationMsg, false,false);
                messageType=jsonObject.getJSONObject("type").getJSONObject("data").getString("msg_type");
                message=jsonObject.getJSONObject("type").getJSONObject("data").getString("message");
                urlstring=jsonObject.getJSONObject("type").getJSONObject("data").getString("url");

                if (data.containsKey("hc_type") && data.containsKey("chat_id")) {
                    sendNotification(notificationMsg, true, false,"");
                } else if (jsonObject.getJSONObject("type").has("type") && jsonObject.getJSONObject("type").getString("type").equalsIgnoreCase("BROADCAST")) {
                    sendNotification(notificationMsg, false, true,messageType);
                } else {
                    sendNotification(notificationMsg, false, false,"");
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.d("message", data.get("message"));
                notificationMsg = data.get("message").toString();
                if (data.containsKey("hc_type") && data.containsKey("chat_id")) {
                    sendNotification(notificationMsg, true, false,"");
                } else if (data.containsKey("type") && data.get("type").equalsIgnoreCase("BROADCAST")) {
                    sendNotification(notificationMsg, false, true,messageType);
                } else {
                    sendNotification(notificationMsg, false, false,"");
                }
            }
        }

    }

    @Override
    public void onNewToken(String token) {
        Preferences.setPreferences(PreferencesConstants.DEVICE_TOKEN, token);
    }


    public void sendNotification(String notificationMsg, boolean isFromHelpCranch, Boolean isBroadcatsMessage,String messageType) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Hello";// The user-visible name of the channel.
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            mNotificationManager.createNotificationChannel(mChannel);
        }
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            NotificationCompat.Builder mBuilder;
        if (isFromHelpCranch) {
            notificationIntent = new Intent(getApplicationContext(), BaseActivity.class).putExtra("from", "notification");
            //    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.tvam_logo)
                        //   .setContentTitle("Tvam (त्वम्) - Vedic Astrology & You")
                        //  .setContentTitle("Tvam (त्वम्) - Vedic Astrology & You")

                        .setContentText("[Tvam (त्वम्)]: " + notificationMsg)
                        .setSound(uri)
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setContentIntent(contentIntent)
                        .setChannelId(CHANNEL_ID);
    } else if (isBroadcatsMessage && !isFromHelpCranch) {
        notificationIntent = new Intent(getApplicationContext(), BaseActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TASK).
        putExtra("from", "notification_3").
        putExtra("type", messageType).
        putExtra("url", urlstring).
        putExtra("content", message);
        contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.tvam_logo)
                        //  .setContentTitle("Tvam (त्वम्) - Vedic Astrology & You")
                        .setContentText("[Tvam (त्वम्)]: " + notificationMsg)
                        //  .setContentText(notificationMsg)
                        .setContentIntent(contentIntent)
                        .setSound(uri)
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setChannelId(CHANNEL_ID);
    } else {
        notificationIntent = new Intent(getApplicationContext(), BaseActivity.class).putExtra("from", "notification_2");
     //   notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.tvam_logo)
                        //  .setContentTitle("Tvam (त्वम्) - Vedic Astrology & You")
                        .setContentText("[Tvam (त्वम्)]: " + notificationMsg)
                        //  .setContentText(notificationMsg)
                        .setContentIntent(contentIntent)
                        .setSound(uri)
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setChannelId(CHANNEL_ID);
    }
    //    mNotificationManager.notify();
    mNotificationManager.notify(001, mBuilder.build());
}


@SuppressLint("InvalidWakeLockTag")
public static void acquire(Context context) {
    if (wakeLock != null)
        wakeLock.release();
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "WakeLock");
    wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/);
}

public static void release() {
    if (wakeLock != null)
        wakeLock.release();
    wakeLock = null;
}

0 个答案:

没有答案