新手在这里。
我开发了一个使用firebase数据库和android studio的聊天应用程序,并使用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 ());
}
}
答案 0 :(得分:0)
正如Rabbit here先生回答的那样,您可以在扩展FirebaseMessagingService的类中实现isAppIsBackgrounded()方法:
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是否处于活动状态。