禁止仅在后台接收通知
Posted
技术标签:
【中文标题】禁止仅在后台接收通知【英文标题】:Disable notifications to be received only in background 【发布时间】:2018-07-04 10:20:15 【问题描述】:我使用 firebase 云消息传递和 firebase 功能构建了一个聊天应用程序。但我目前有两个问题。 1.当应用程序打开并有新消息进来时,应用程序将自动移动到应用程序的主活动。 2. 其次,我希望仅在后台接收通知。
我该如何实现?
下面是我的 onMessageReceived
public class MyFirebaseMessagingService extends FirebaseMessagingService
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0)
showNotification(remoteMessage.getData().get("name"), (remoteMessage.getData().get("click_action")), remoteMessage.getData().get("title"));
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null)
private void showNotification(String name, String click_action, String title)
Intent intent;
if (click_action.equals("Download"))
intent = new Intent(this, Download.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
else if (click_action.equals("Student_SystemsDevt"))
intent = new Intent(this, Student_SystemsDevt.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
else
intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(name)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle())
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
我的清单文件
<application
android:name=".GTUCONLINE"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".LoginActivity"
android:screenOrientation="portrait"
android:theme="@style/MyTheme" />
<activity android:name=".MainActivity">
</activity>
<activity
android:name=".ChoiceActivity"
android:screenOrientation="portrait"
android:theme="@style/MyTheme" />
<activity
android:name=".WelcomeActivity"
android:screenOrientation="portrait"
android:theme="@style/MyTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat" />
<activity
android:name=".StudentSignUp"
android:screenOrientation="portrait"
android:theme="@style/MyTheme" />
<activity
android:name=".LecturerSignUp"
android:screenOrientation="portrait" />
<activity android:name=".ProgrammeActivity" />
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity
android:name=".TopicActivity"
android:theme="@style/MyTheme" />
<activity android:name=".MIS" />
<activity android:name=".GB" />
<activity android:name=".FIN" />
<activity android:name=".ENGINEERINGM" />
<activity android:name=".BDM" />
<activity android:name=".SCM" />
<activity android:name=".TE" />
<activity android:name=".TM" />
<activity android:name=".BET" />
<activity android:name=".ICT" />
<activity android:name=".T" />
<activity android:name=".IT" />
<activity android:name=".AM" />
<activity android:name=".OGM" />
<activity android:name=".I" />
<activity android:name=".PEF" />
<activity android:name=".QM" />
<activity android:name=".EPM" />
<activity android:name=".PM" />
<activity android:name=".HM" />
<activity android:name=".StudentsList">
<intent-filter>
<action android:name="StudentsList" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".LecturersList" />
<activity android:name=".AdminActivity" />
<activity
android:name=".ChatActivity"
android:parentActivityName=".StudentsList" />
<activity android:name=".LecturerMainActivity" />
<activity
android:name=".Download"
android:parentActivityName=".MIS">
<intent-filter>
<action android:name="Download" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MIS_Information"
android:parentActivityName=".MIS" />
<activity android:name=".Terms" />
<activity
android:name=".Admin_Login"
android:parentActivityName=".LoginActivity" />
<activity
android:name=".Manipulation"
android:parentActivityName=".Admin_Login" />
<activity android:name=".Admins" />
<activity
android:name=".SystemsDevt"
android:parentActivityName=".MIS" />
<activity
android:name=".SysGroupChat"
android:parentActivityName=".SystemsDevt" />
<activity
android:name=".EntSystems"
android:parentActivityName=".MIS" />
<activity android:name=".DownloadEnt">
<intent-filter>
<action android:name="DownloadEnt" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".student_mis" />
<activity
android:name=".Student_SystemsDevt"
android:parentActivityName=".student_mis">
<intent-filter>
<action android:name="Student_SystemsDevt" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Student_EntSys"
android:parentActivityName=".student_mis" />
<activity
android:name=".SystemsDevt_Add"
android:parentActivityName=".SystemsDevt" />
<activity android:name=".EntSystemsAdd" />
<activity android:name=".EntSystemsInfo" />
<activity
android:name=".DatabaseSystems"
android:parentActivityName=".MIS" />
<activity android:name=".DownloadDatabaseSys" />
<activity
android:name=".Student_DatabaseSystems"
android:parentActivityName=".student_mis" />
<activity android:name=".DatabaseInfo" />
<activity
android:name=".DatabaseAdd"
android:parentActivityName=".DatabaseSystems" />
<activity
android:name=".IntroToOil"
android:parentActivityName=".GB" />
<activity
android:name=".DownloadIntro"
android:parentActivityName=".IntroToOil" />
<activity
android:name=".IntroToOilInfo"
android:parentActivityName=".IntroToOil" />
<activity
android:name=".IntroToOilAdd"
android:parentActivityName=".IntroToOil" />
<activity android:name=".student_gb" />
<activity android:name=".StudentIntro" />
<activity android:name=".MIS_students_new"
android:parentActivityName=".SystemsDevt">
</activity>
</application>
【问题讨论】:
您的应用收到数据通知还是简单推送通知? 收到数据通知 这个答案会帮助你***.com/a/44455118/4919210 【参考方案1】:1- 实际上 onMessageReceived 仅在您的应用程序处于前台时触发,如enter link description here 所述。所以如果你不覆盖onMessageReceived,当你的应用在前台时你将不会收到推送
2- 根据 fcm 文档,当您的应用处于后台时,不会触发 onMessageReceived,但
数据负载在您的启动器 Activity 的意图的附加内容中传递。
它解释了为什么您的推送重定向到您的主要活动(您的启动器)。
到目前为止,您可以处理来自意图的重定向。
您还可以指定将其重定向到的活动,例如enter link description here:
"to":"some_device_token",
"content_available": true,
"notification":
"title": "hello",
"body": "test message",
"click_action": "OPEN_ACTIVITY_1"
,
"data":
"extra":"juice"
不要忘记所需活动的意图过滤器:
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
【讨论】:
那么如何修复自动重定向? 在“click_action”字段中指定您的活动 目前正在尝试。一分钟 自动重定向仍然持续 我不关心用户单击通知以打开特定活动。我想知道当用户在另一个活动中时如何解决自动重定向的问题。该应用程序总是在弹出通知之前将用户重定向回主要活动。我该如何解决?【参考方案2】:还在清单中添加任务权限。
private boolean isAppForeground()
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null)
return false;
final String packageName = getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses)
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName))
return true;
return false;
if(isAppForeground())
// Handle notification silently without displaying in notification tray
else
// Do your regular stuff
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0)
showNotification(remoteMessage.getData().get("name"), (remoteMessage.getData().get("click_action")), remoteMessage.getData().get("title"));
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null)
【讨论】:
这个怎么用? 类 ForegroundCheckTask 应放在 MyFirebaseMessagingService 类之外,其余代码应放在 onMessageRecived() 中 现在试试我已经删除了上下文。 我没有收到通知,但是当我在应用程序中时,我被重定向回主要活动。有趣的是,这不是我的启动器 你是如何在 if() 条件下处理静默通知的?因为挂起的 Intent 也可能是跳转的原因。以上是关于禁止仅在后台接收通知的主要内容,如果未能解决你的问题,请参考以下文章