Android FCM在应用程序被杀死时部分工作。

huangapple go评论114阅读模式
英文:

Android FCM working partly while app is killed

问题

以下是您要翻译的内容:

我在我的 MyFirebaseMessagingService.java 中有两种通知构建器,一种传递名为 'lecture_date' 的字符串,并在单击时打开 Lectures_graph.class,而另一种传递名为 'web_url' 的字符串,并在单击时打开 webview_base.class。

我想要实现的目标是:即使应用程序被关闭,也要完全工作的 FCM 通知,通知在单击后必须打开活动(带有额外信息)。

我目前的进展:我可以接收通知以打开 Lectures_graph.class,并带有意图附加项,一切都加载并正常工作。即使我接收到打开 webview_base.class 活动的通知,单击通知时什么都不会发生。

可能有一件事情指向问题 - Logcat 经常告诉我:

E/FirebaseMessaging: 通知挂起意图已取消

我使用 PHP 发送通知(该代码是 100% 正确和工作的),下面的代码代表了发送给 webview_base.class 活动的 JSON 的想法。要发送通知以打开 Lectures_graph.class 活动,我只需将 click_action 从 "OPEN_ACTIVITY_WEBVIEW" 更改为 "OPEN_ACTIVITY_LECTURES" 并更改数据有效载荷。用于 webview_base.class 活动的 PHP 发送这样的数据:

  1. $msg = array
  2. (
  3. 'body' => 'test body',
  4. 'title' => "test title",
  5. 'click_action' => 'OPEN_ACTIVITY_WEBVIEW',
  6. 'channelId' => "newsfeed",
  7. 'vibrate' => 1,
  8. 'sound' => 1
  9. );
  10. //通过主题发送通知
  11. $fields = array
  12. (
  13. "data" => [
  14. 'web_url' => $_POST['notification_url']
  15. ],
  16. 'notification' => $msg,
  17. 'to' => '/topics/test'
  18. );

这是我的整个 Android Studio 清单文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. package="secret">
  5. <uses-permission android:name="android.permission.INTERNET" />
  6. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  7. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  8. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  9. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
  10. <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
  11. <uses-permission android:name="android.permission.WAKE_LOCK" />
  12. <supports-screens
  13. android:smallScreens="true"
  14. android:normalScreens="true"
  15. android:largeScreens="true"
  16. android:anyDensity="true" />
  17. <application
  18. android:allowBackup="false"
  19. android:icon="@mipmap/ic_launcher"
  20. android:label="@string/app_name"
  21. android:roundIcon="@mipmap/ic_launcher_round"
  22. android:theme="@style/AppTheme.CustomTheme"
  23. android:usesCleartextTraffic="true"
  24. tools:targetApi="m">
  25. <activity
  26. android:name=".systems.webview_activity.webview_base"
  27. android:configChanges="orientation|screenSize">
  28. <intent-filter>
  29. <category android:name="android.intent.category.BROWSABLE" />
  30. <action android:name="android.intent.action.VIEW" />
  31. <data android:scheme="file" />
  32. <data android:mimeType="*/*" />
  33. <data android:pathPattern=".*\.kdb" />
  34. <data android:host="*" />
  35. <!-- 即使此方法适用于 Lectures_graph 活动,但对于此方法无效 -->
  36. <action android:name="OPEN_ACTIVITY_WEBVIEW" />
  37. <category android:name="android.intent.category.DEFAULT" />
  38. </intent-filter>
  39. </activity>
  40. <activity
  41. android:name=".systems.lectures.Lectures_graph"
  42. android:configChanges="keyboardHidden|screenSize"
  43. android:label="@string/title_lectures_graph"
  44. >
  45. <intent-filter>
  46. <!-- 此意图过滤器对于提供在我的 PHP 文件中的 click_action 的正确响应 -->
  47. <action android:name="OPEN_ACTIVITY_LECTURES" />
  48. <action android:name="android.intent.action.CREATE_SHORTCUT" />
  49. <category android:name="android.intent.category.DEFAULT" />
  50. </intent-filter>
  51. </activity>
  52. <activity
  53. android:name=".SplashScreen"
  54. android:configChanges="keyboardHidden|orientation|screenSize" >
  55. <intent-filter>
  56. <action android:name="android.intent.action.MAIN" />
  57. <category android:name="android.intent.category.LAUNCHER" />
  58. </intent-filter>
  59. </activity>
  60. <activity
  61. android:name=".NoInternetConnection"
  62. android:configChanges="keyboardHidden|orientation|screenSize" />
  63. <activity
  64. android:name="secret.systems.about.about_app"
  65. android:configChanges="orientation|keyboardHidden|screenSize"
  66. android:theme="@style/FullscreenTheme" />
  67. <activity
  68. android:name=".MainActivity"
  69. android:launchMode="singleTask"
  70. android:configChanges="orientation|keyboardHidden|screenSize">
  71. </activity>
  72. <service
  73. android:name="secret.services.MyFirebaseMessagingService"
  74. android:enabled="true"
  75. android:exported="false">
  76. <intent-filter>
  77. <action android:name="com.google.firebase.MESSAGING_EVENT" />
  78. </intent-filter>
  79. </service>
  80. </application>
  81. </manifest>

MyFirebaseMessagingService.java:

  1. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  2. private static final String TAG = "MyFirebaseMsgService";
  3. @Override
  4. public void onMessageReceived(RemoteMessage remoteMessage) {
  5. // 检查消息是否包含通知负载。
  6. if (remoteMessage.getNotification() != null) {
  7. Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
  8. // 从通知中获取键/值
  9. String lectureDate = remoteMessage.getData().get("lecture_date");
  10. String web_url = remoteMessage.getData().get("web_url");
  11. if(lectureDate != null)sendLecturesNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), lectureDate);
  12. else if(web_url != null) sendNotificationWithURL(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), web_url);
  13. else Log.e(TAG, "Message.notification is empty!");
  14. }
  15. }
  16. @Override
  17. public void onNewToken(@NonNull String token) {
  18. Log.d
  19. <details>
  20. <summary>英文:</summary>
  21. I have 2 kinds of notification builders in my MyFirebaseMessagingService.java, one passes string called &#39;lecture_date&#39; and opens Lectures_graph.class while the other one passes string called &#39;web_url&#39; and opens webview_base.class when clicked.
  22. What i want to achieve : fully working FCM notifications even when app is killed, notifications must open activity (with extras) once they are clicked.
  23. How far i am : I can receive notifications to open Lectures_graph.class with intent extras, everything loads and works just fine. Even if i receive notifications for webview_base.class activity, nothing happens when i click on notification.
  24. There might be one thing that points to problem - Logcat often tells me this :
  25. E/FirebaseMessaging: Notification pending intent canceled
  26. Im using PHP to send out notifications (that code is 100% correct and working), code below shows represents the idea of JSON sent out for webview_base.class activity. To send out notifications for Lectures_graph.class activity, i just have to swap click_action from &quot;OPEN_ACTIVITY_WEBVIEW&quot; to &quot;OPEN_ACTIVITY_LECTURES&quot; and change the data payload. PHP for webview_base.class activity sends away such data :
  27. $msg = array
  28. (
  29. &#39;body&#39; =&gt; &#39;test body&#39;,
  30. &#39;title&#39; =&gt; &quot;test title&quot;,
  31. &#39;click_action&#39; =&gt; &#39;OPEN_ACTIVITY_WEBVIEW&#39;,
  32. &#39;channelId&#39; =&gt; &quot;newsfeed&quot;,
  33. &#39;vibrate&#39; =&gt; 1,
  34. &#39;sound&#39; =&gt; 1
  35. );
  36. //sends notifications via topics
  37. $fields = array
  38. (
  39. &quot;data&quot; =&gt; [
  40. &#39;web_url&#39; =&gt; $_POST[&#39;notification_url&#39;]
  41. ],
  42. &#39;notification&#39; =&gt; $msg,
  43. &#39;to&#39; =&gt; &#39;/topics/test&#39;
  44. );
  45. This is my whole manifest file from Android studio :
  46. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  47. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  48. package=&quot;secret&quot;&gt;
  49. &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
  50. &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
  51. &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
  52. &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
  53. &lt;uses-permission android:name=&quot;com.android.launcher.permission.INSTALL_SHORTCUT&quot;/&gt;
  54. &lt;uses-permission android:name=&quot;com.google.android.c2dm.permission.RECEIVE&quot; /&gt;
  55. &lt;uses-permission android:name=&quot;android.permission.WAKE_LOCK&quot; /&gt;
  56. &lt;supports-screens
  57. android:smallScreens=&quot;true&quot;
  58. android:normalScreens=&quot;true&quot;
  59. android:largeScreens=&quot;true&quot;
  60. android:anyDensity=&quot;true&quot; /&gt;
  61. &lt;application
  62. android:allowBackup=&quot;false&quot;
  63. android:icon=&quot;@mipmap/ic_launcher&quot;
  64. android:label=&quot;@string/app_name&quot;
  65. android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
  66. android:theme=&quot;@style/AppTheme.CustomTheme&quot;
  67. android:usesCleartextTraffic=&quot;true&quot;
  68. tools:targetApi=&quot;m&quot;&gt;
  69. &lt;activity
  70. android:name=&quot;.systems.webview_activity.webview_base&quot;
  71. android:configChanges=&quot;orientation|screenSize&quot;&gt;
  72. &lt;intent-filter&gt;
  73. &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
  74. &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
  75. &lt;data android:scheme=&quot;file&quot; /&gt;
  76. &lt;data android:mimeType=&quot;\*/\*&quot; /&gt;
  77. &lt;data android:pathPattern=&quot;.*\\.kdb&quot; /&gt;
  78. &lt;data android:host=&quot;*&quot; /&gt;
  79. //even if this method worked with Lectures_graph activity
  80. //it doesnt work with this one
  81. &lt;action android:name=&quot;OPEN_ACTIVITY_WEBVIEW&quot; /&gt;
  82. &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
  83. &lt;/intent-filter&gt;
  84. &lt;/activity&gt;
  85. &lt;activity
  86. android:name=&quot;.systems.lectures.Lectures_graph&quot;
  87. android:configChanges=&quot;keyboardHidden|screenSize&quot;
  88. android:label=&quot;@string/title_lectures_graph&quot;
  89. &gt;
  90. &lt;intent-filter&gt;
  91. //this intent filter provides correct response
  92. //to click_action which is provided in my PHP file
  93. &lt;action android:name=&quot;OPEN_ACTIVITY_LECTURES&quot; /&gt;
  94. &lt;action android:name = &quot;android.intent.action.CREATE_SHORTCUT&quot; /&gt;
  95. &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
  96. &lt;/intent-filter&gt;
  97. &lt;/activity&gt;
  98. &lt;activity
  99. android:name=&quot;.SplashScreen&quot;
  100. android:configChanges=&quot;keyboardHidden|orientation|screenSize&quot; &gt;
  101. &lt;intent-filter&gt;
  102. &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  103. &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
  104. &lt;/intent-filter&gt;
  105. &lt;/activity&gt;
  106. &lt;activity
  107. android:name=&quot;.NoInternetConnection&quot;
  108. android:configChanges=&quot;keyboardHidden|orientation|screenSize&quot; /&gt;
  109. &lt;activity
  110. android:name=&quot;secret.systems.about.about_app&quot;
  111. android:configChanges=&quot;orientation|keyboardHidden|screenSize&quot;
  112. android:theme=&quot;@style/FullscreenTheme&quot; /&gt;
  113. &lt;activity
  114. android:name=&quot;.MainActivity&quot;
  115. android:launchMode=&quot;singleTask&quot;
  116. android:configChanges=&quot;orientation|keyboardHidden|screenSize&quot;&gt;
  117. &lt;/activity&gt;
  118. &lt;service
  119. android:name=&quot;secret.services.MyFirebaseMessagingService&quot;
  120. android:enabled=&quot;true&quot;
  121. android:exported=&quot;false&quot;&gt;
  122. &lt;intent-filter&gt;
  123. &lt;action android:name=&quot;com.google.firebase.MESSAGING_EVENT&quot; /&gt;
  124. &lt;/intent-filter&gt;
  125. &lt;/service&gt;
  126. &lt;/application&gt;
  127. &lt;/manifest&gt;
  128. MyFirebaseMessagingService.java:
  129. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  130. private static final String TAG = &quot;MyFirebaseMsgService&quot;;
  131. @Override
  132. public void onMessageReceived(RemoteMessage remoteMessage) {
  133. // Check if message contains a notification payload.
  134. if (remoteMessage.getNotification() != null) {
  135. Log.d(TAG, &quot;Message Notification Body: &quot; + remoteMessage.getNotification().getBody());
  136. //get key/value from notification
  137. String lectureDate = remoteMessage.getData().get(&quot;lecture_date&quot;);
  138. String web_url = remoteMessage.getData().get(&quot;web_url&quot;);
  139. if(lectureDate != null)sendLecturesNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), lectureDate);
  140. else if(web_url != null) sendNotificationWithURL(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), web_url);
  141. else Log.e(TAG, &quot;Message.notification is empty!&quot;);
  142. }
  143. }
  144. @Override
  145. public void onNewToken(@NonNull String token) {
  146. Log.d(TAG, &quot;Refreshed token: &quot; + token);
  147. }
  148. private void sendLecturesNotification(String title,String messageBody, String date) {
  149. Intent intent;
  150. intent = new Intent(this, Lectures_graph.class).putExtra(&quot;lecture_date&quot;, date).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  151. PendingIntent pendingIntent = PendingIntent.getActivity(this, ((int) System.currentTimeMillis()) /* Request code */, intent,
  152. 0);
  153. String channelId = getString(R.string.lectures_channel_id);
  154. Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  155. NotificationCompat.Builder notificationBuilder =
  156. new NotificationCompat.Builder(this, channelId)
  157. .setSmallIcon(R.drawable.secret)
  158. .setContentTitle(title)
  159. .setContentText(messageBody)
  160. .setAutoCancel(true)
  161. .setSound(defaultSoundUri)
  162. .setContentIntent(pendingIntent);
  163. NotificationManager notificationManager =
  164. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  165. if (notificationManager != null) {
  166. notificationManager.notify(((int) System.currentTimeMillis()), notificationBuilder.build());
  167. }
  168. Log.d(TAG, &quot;Lectures notification built with date:&quot;+date);
  169. }
  170. private void sendNotificationWithURL(String title, String messageBody, String web_url) {
  171. Intent intent;
  172. intent = new Intent(this, webview_base.class).putExtra(&quot;web_url&quot;, web_url).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  173. PendingIntent pendingIntent = PendingIntent.getActivity(this, ((int) System.currentTimeMillis()) /* Request code */, intent,
  174. 0);
  175. String channelId = getString(R.string.newsfeed_channel_id);
  176. Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  177. NotificationCompat.Builder notificationBuilder =
  178. new NotificationCompat.Builder(this, channelId)
  179. .setSmallIcon(R.drawable.secret)
  180. .setContentTitle(title)
  181. .setContentText(messageBody)
  182. .setAutoCancel(true)
  183. .setSound(defaultSoundUri)
  184. .setContentIntent(pendingIntent);
  185. NotificationManager notificationManager =
  186. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  187. if (notificationManager != null) {
  188. notificationManager.notify(((int) System.currentTimeMillis()), notificationBuilder.build());
  189. }
  190. Log.d(TAG, &quot;Webview notification built with URL:&quot;+web_url);
  191. }
  192. }
  193. a while ago i was getting an error in logcat that said something like &quot;default FCM channel is not defined&quot;, but im not quite sure thats the problem.
  194. Even if i searched across the whole web, i would like to see better solutions/suggestions for notification (with its payload) handling when app is killed.
  195. </details>
  196. # 答案1
  197. **得分**: 0
  198. 这是我修复了噩梦的方法
  199. 由于我无法从通知中打开多个特定活动为什么不直接打开我的应用程序主类并从那里处理额外的变量呢
  200. 不再为每个想要的活动添加以下内容
  201. ```xml
  202. <intent-filter>
  203. <action android:name="OPEN_ACTIVITY_???????" />
  204. <category android:name="android.intent.category.DEFAULT" />
  205. </intent-filter>

我做了这个:

  1. <activity
  2. android:name=".MainActivity"
  3. android:launchMode="singleTask"
  4. android:configChanges="orientation|keyboardHidden|screenSize">
  5. <intent-filter>
  6. <action android:name="OPEN_APP" />
  7. <category android:name="android.intent.category.DEFAULT" />
  8. </intent-filter>
  9. </activity>

我在通知负载中添加了额外的变量(例如:'activity' => 'webview'),并在我的PHP代码中重命名了click_action。我在MainActivity.class中处理了所有负载,并从那里调用了所需的Android活动。MainActivity 代码:

  1. Bundle extras = getIntent().getExtras();
  2. if(extras == null) {
  3. Log.e("MainActivity", "No Activities passed in notification extras");
  4. } else {
  5. //从通知负载中获取要打开的活动
  6. String activity = extras.getString("activity");
  7. if( activity != null){
  8. switch (activity){
  9. //webview 活动
  10. case "webview":
  11. //获取 webview 的 URL
  12. String web_url = extras.getString("web_url");
  13. if(web_url != null){
  14. finish();
  15. startActivity(new Intent(this, webview_base.class).putExtra("web_url", web_url));
  16. }
  17. break;
  18. //lectures 活动
  19. case "lectures":
  20. //获取讲座日期
  21. String lecture_date = extras.getString("lecture_date");
  22. if(lecture_date != null){
  23. finish();
  24. startActivity(new Intent(this, Lectures_graph.class).putExtra("lecture_date", lecture_date));
  25. }
  26. break;
  27. }
  28. }
  29. }

希望这对某天的某人有所帮助。

干杯!

英文:

So i fixed my nightmare and here is how :

Since i cant open more than one specific activity from notifications, why could i just open my apps main class and handle extra variable from there ?

Instead of having for each wanted activity

  1. &lt;intent-filter&gt;
  2. &lt;action android:name=&quot;OPEN_ACTIVITY_???????&quot; /&gt;
  3. &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
  4. &lt;/intent-filter&gt;

i made this :

  1. &lt;activity
  2. android:name=&quot;.MainActivity&quot;
  3. android:launchMode=&quot;singleTask&quot;
  4. android:configChanges=&quot;orientation|keyboardHidden|screenSize&quot;&gt;
  5. &lt;intent-filter&gt;
  6. &lt;action android:name=&quot;OPEN_APP&quot; /&gt;
  7. &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
  8. &lt;/intent-filter&gt;
  9. &lt;/activity&gt;

I added extra variable to notifications payload (examle : 'activity' => 'webview') and renamed click_action in my PHP code. I handled all payloads in MainActivity.class and called wanted android activity from there. MainActivity code :

  1. Bundle extras = getIntent().getExtras();
  2. if(extras == null) {
  3. Log.e(&quot;MainActivity&quot;, &quot;No Activities passed in notification extras&quot;);
  4. } else {
  5. //get opening activity from notification payload
  6. String activity = extras.getString(&quot;activity&quot;);
  7. if( activity != null){
  8. switch (activity){
  9. //webview activity
  10. case &quot;webview&quot;:
  11. //gets url for webview
  12. String web_url = extras.getString(&quot;web_url&quot;);
  13. if(web_url != null){
  14. finish();
  15. startActivity(new Intent(this, webview_base.class).putExtra(&quot;web_url&quot;, web_url));
  16. }
  17. break;
  18. //lectures activity
  19. case &quot;lectures&quot;:
  20. //gets lecture date
  21. String lecture_date = extras.getString(&quot;lecture_date&quot;);
  22. if(lecture_date != null){
  23. finish();
  24. startActivity(new Intent(this, Lectures_graph.class).putExtra(&quot;lecture_date&quot;, lecture_date));
  25. }
  26. break;
  27. }
  28. }
  29. }

Hope this helps someone, someday..

Cheers.

huangapple
  • 本文由 发表于 2020年8月4日 01:07:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63233910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定