如何在android应用程序活动和非活动状态下处理推送通知



hi如何处理推送通知点击我需要打开带有特定片段的活动,我有两种状态,比如应用程序关闭和应用程序打开,当应用程序关闭时,我需要用MyFragment.class打开主页活动,如果应用程序被打开,我需要显示对话框u需要查看详细信息,如果是,我需要打开特定的片段,比如MyFragment,如果否,我需要取消对话框,如何实现它…

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

感谢

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

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

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

相关内容

  • 没有找到相关文章

最新更新