gpt4 book ai didi

Android GCM "background"通知在单击时不会启动我的应用程序

转载 作者:行者123 更新时间:2023-12-05 07:47:48 25 4
gpt4 key购买 nike

当我收到“通知”GCM 消息时,android 会自动根据标题和正文创建通知,并将其放入通知栏中。

当我在这里说“通知”时,我指的是本文档中称为通知的通知 https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages

我遇到的问题是,当我点击它时,没有任何反应 - 它没有启动我的 Activity ,也没有关闭通知。

我试过使用“click_action”选项,但没有效果。我添加了一个“数据”部分,但没有任何效果。

当我的应用程序处于前台时,我会按预期收到 onMessageReceived,并且我在其中构建的 NotificationCompat 会按预期重新启动应用程序。

我缺少什么来获取启动应用程序的后台通知?

这是 list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="example.package"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />

<!-- [START gcm_permission] -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- [END gcm_permission] -->

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<permission
android:name="example.package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />

<uses-permission android:name="example.package.permission.C2D_MESSAGE" />



<application
android:name="example.package.application.appApplication"
android:allowBackup="true"
android:icon="@drawable/app_app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme"
tools:replace="android:icon, allowBackup"
>
<activity
android:name="example.package.main.activity.MainActivity"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- [START gcm_receiver] -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="example.package" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->


<!-- [START gcm_listener] -->
<service
android:name="example.package.service.MyGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- [END gcm_listener] -->
<!-- [START instanceId_listener] -->
<service
android:name="example.package.service.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<!-- [END instanceId_listener] -->
<service
android:name="example.package.service.RegistrationIntentService"
android:exported="false"></service>

</application>

</manifest>

这是我发送给 GCM 的示例 json:

{
"to" : "<snipped>",
"notification" : {
"title" : "<snipped>",
"body" : "<snipped>",
"icon" : "ic_launcher"

},
"data":{"a":"b"},
"content_available": true,
"click_action":"android.intent.action.MAIN"
}

当我单击通知窗口中的项目时,通过详细登录得到的唯一结果是:

08-30 11:54:35.942 3684-3980/? D/InputReader: Input event(4): value=1 when=7306609326000
08-30 11:54:35.942 3684-3980/? D/InputReader: Input event(4): value=1 when=7306609326000
08-30 11:54:35.942 3684-3980/? I/InputReader: Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.323 ] when=7306609326000
08-30 11:54:35.952 3684-3980/? D/InputReader: lastThreadEndTime = 7303509457022, currentThreadStartTime = 7303509466814, diff = 0
08-30 11:54:35.952 3684-3979/? I/InputDispatcher: Delivering touch to (4091): action: 0x0, toolType: 1
08-30 11:54:35.952 4091-4091/? D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
08-30 11:54:35.952 3684-4035/? D/lights: button : 1 +
08-30 11:54:35.992 3684-4035/? D/lights: button : 1 -
08-30 11:54:36.112 3684-3980/? D/InputReader: Input event(4): value=0 when=7306778958000
08-30 11:54:36.112 3684-3980/? D/InputReader: Input event(4): value=0 when=7306778958000
08-30 11:54:36.112 3684-3980/? I/InputReader: Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=] when=7306778958000
08-30 11:54:36.112 3684-3979/? I/InputDispatcher: Delivering touch to (4091): action: 0x1, toolType: 1

这是在 Marshmallow 和 JellyBean 设备上发生的,尽管我没有在两者之间进行任何测试。

我知道我可以单独发送“数据”元素,即使在后台也可以启动我的服务,但如果应用程序被强制停止,那么它的缺点是无法工作。我真的想弄清楚我需要做什么才能获得 Google 在该链接上描述的行为。

这是 MyGcmListenerService - 当应用程序在前台时它确实被正确调用。

public class MyGcmListenerService extends GcmListenerService {

private static final String TAG = "MyGcmListenerService";

/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
// String message = data.getString("message");
String message = data.getBundle("notification").getString("body");
String customFields = data.getBundle("notification").getString("customFields");
String title = data.getBundle("notification").getString("title");
Log.d("From: " + from);
Log.d( "Title: " + title);
Log.d( "Message: " + message);
Log.d( "CustomFields: " + customFields);


if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}

// [START_EXCLUDE]
/**
* Production applications would usually process the message here.
* Eg: - Syncing with server.
* - Store message in local database.
* - Update UI.
*/

/**
* In some cases it may be useful to show a notification indicating to the user
* that a message was received.
*/
sendNotification(message);
// [END_EXCLUDE]
}
// [END receive_message]

/**
* Create and show a simple notification containing the received GCM message.
*
* @param message GCM message received.
*/
private void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
/* resumes the current activity - just like pressing link on home page */


PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}

为了澄清,我正在专门研究如何让这种行为与 GCM 一起工作:

根据 https://developers.google.com/cloud-messaging/downstream '当您的应用程序处于后台时,Android 会将带有通知的消息定向到系统托盘。默认情况下,用户点击通知会打开应用启动器。”

我已经能够用 FCM 重写它,但我没有看到这个问题,但我真的很想知道我错过了什么。

最佳答案

基于此related SO question ,在 JSON 中,“notification”用于发送简单的通知,无需操作。

根据这个documentation :

Notifications provide an easy way for developers to send a user-visible display message with some predefined keys and optional custom key/value pairs. Data payloads include the developer’s custom key/value pairs only, and the client app must handle the message. You can send messages that have both notification and data payloads.

SO question还建议将消息​​接收器中的 MainActivity.class 替换为入口 Activity 。

final Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

您也可以查看此 link带有有关单击通知时如何打开应用程序的示例代码。希望这对您有所帮助!

关于Android GCM "background"通知在单击时不会启动我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39233127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com