聊天应用程序 - 如何在聊天活动处于前台时停止推送通知



>新手在这里。

我开发了一个使用Firebase数据库和android工作室的聊天应用程序,并使用Firebase功能以及Firebase消息服务在用户收到新消息时向他们发送通知。一切正常。我唯一的问题是我想在两个用户已经在聊天时停止通知服务(ChatActivity 已经在运行(。

我到处找过,但找不到解决方案提前感谢!

@Override
public void onMessageReceived( RemoteMessage remoteMessage ) {

        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){


                super.onMessageReceived ( remoteMessage );

                String notification_title = remoteMessage.getData ().get ( "title" );
                String notification_message = remoteMessage.getData ().get ( "body" );

                String click_action = remoteMessage.getData ().get ( "click_action" );
                String visit_user_id = remoteMessage.getData ().get ( "from_user_id" );
                String visit_user_name = remoteMessage.getData ().get ( "userName" );
                Uri alarmSound = Uri.parse ("android.resource://"+getApplicationContext ().getPackageName()+"/"+R.raw.dog  );
                Intent resultIntent = new Intent ( click_action );
                resultIntent.putExtra ( "visit_user_id", visit_user_id );
                resultIntent.putExtra ( "userName", visit_user_name );
                PendingIntent resultPendingIntent =
                        PendingIntent.getActivity (
                                this,
                                0,
                                resultIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT
                        );

                OreoNotification oreoNotification = new OreoNotification ( this );
                Notification.Builder builder = oreoNotification.getOreoNotification ( notification_title, notification_message,resultPendingIntent ).setSmallIcon ( R.drawable.finalpawslogofour )
                        .setVibrate ( new long[]{500, 500} ).setAutoCancel ( true ).setSound ( alarmSound );

                int mNotificationId = ( int ) System.currentTimeMillis ();

                oreoNotification.getManager().notify ( mNotificationId, builder.build ());

        }else{

                super.onMessageReceived ( remoteMessage );

                String notification_title = remoteMessage.getData ().get ( "title" );
                String notification_message = remoteMessage.getData ().get ( "body" );

                String click_action = remoteMessage.getData ().get ( "click_action" );
                String visit_user_id = remoteMessage.getData ().get ( "from_user_id" );
                String visit_user_name = remoteMessage.getData ().get ( "userName" );
                Uri alarmSound = Uri.parse ("android.resource://"+getApplicationContext ().getPackageName()+"/"+R.raw.dog  );

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder ( this ).setSmallIcon ( R.drawable.finalpawslogofour ).setContentTitle ( notification_title ).setContentText ( notification_message )
                        .setVibrate ( new long[]{500, 500} ).setAutoCancel ( true ).setSound ( alarmSound );

                Intent resultIntent = new Intent ( click_action );
                resultIntent.putExtra ( "visit_user_id", visit_user_id );
                resultIntent.putExtra ( "userName", visit_user_name );

                PendingIntent resultPendingIntent =
                        PendingIntent.getActivity (
                                this,
                                0,
                                resultIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT
                        );
                mBuilder.setContentIntent ( resultPendingIntent );

                int mNotificationId = ( int ) System.currentTimeMillis ();
                NotificationManager mNotifyMgr = ( NotificationManager ) getSystemService ( NOTIFICATION_SERVICE );
                mNotifyMgr.notify ( mNotificationId, mBuilder.build ());



        }



}

正如 Mr. Rabbit 在这里回答的那样,您可以在您的类中实现 isAppIsBackgrounded(( 方法,该方法扩展了 FirebaseMessagingService:

private boolean isAppIsInBackground(Context context) {
            boolean isInBackground = true;
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
                List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
                for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
                    if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                        for (String activeProcess : processInfo.pkgList) {
                            if (activeProcess.equals(context.getPackageName())) {
                                isInBackground = false;
                            }
                        }
                    }
                }
            }
            else
            {
                List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
                ComponentName componentInfo = taskInfo.get(0).topActivity;
                if (componentInfo.getPackageName().equals(context.getPackageName())) {
                    isInBackground = false;
                }
            }
            return isInBackground;
        }

另一种但粗略的方法是使用SharedPreferences来了解ChatActivity是否处于活动状态。

相关内容

  • 没有找到相关文章

最新更新