【问题标题】:how to handle push notification in android app active and inactive state如何在android应用程序活动和非活动状态下处理推送通知
【发布时间】:2014-04-15 05:54:41
【问题描述】:

您好如何处理推送通知点击我需要打开带有特定片段的活动, 我有两种状态,例如应用程序已关闭和应用程序已打开,当应用程序关闭时,我需要使用 MyFragment.class 打开家庭活动,如果应用程序已打开,则我需要显示对话框,如果是,我需要查看详细信息我需要打开像MyFragment这样的特定片段,如果没有我需要dissmis对话框,,,如何实现它......

@SuppressWarnings("deprecation")
    @Override
    public void update(java.util.Observable observable, Object data) {

        NotificationManager notificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        long when = System.currentTimeMillis();
        Notification notification = new Notification(R.drawable.ic_launcher,
                data.toString(), when);

        String title = this.getString(R.string.app_name);

        Intent notificationIntent = new Intent(this, MainActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(this, title, data.toString(), intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);

        Log.d("GCM_TOKEN", data.toString());

        Toast.makeText(this, "Error: " + data.toString(), Toast.LENGTH_LONG)
                .show();

    }

谢谢

【问题讨论】:

    标签: android push-notification android-notifications android-notification-bar


    【解决方案1】:

    几天前我已经实现了这一点。 首先,我开始使用名为 isRunning 的静态变量来确定活动和非活动状态。 在 Activity onStart()-Method 中,我将 isRunning 设置为 true,当 Activity 进入停止状态时,将变量设置为 false。这样就可以确定活动和非活动状态。

    @Override
    public void onStart() { 
    super.onStart(); 
    mIsRunning = true; 
    }
    
     @Override
    public void onStop() { 
    super.onStop(); 
    mIsRunning = false; 
    }
    

    根据这个变量,您可以根据需要显示您的通知;-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多