gpt4 book ai didi

android - GCM 在收到通知时启动后台/关闭的应用程序 (Android)

转载 作者:行者123 更新时间:2023-11-30 01:29:01 24 4
gpt4 key购买 nike

我对 GCM 有疑问。我创建了一个服务器端通知推送器(在 PHP 中),它向拥有该应用程序的特定用户(基于位置)发送通知。

我有三个类来管理注册和通知监听器。

public class PusherIDListenerService extends InstanceIDListenerService {
private static final String TAG = "PUSHER_ID_LISTENER_SERVICE";

@Override
public void onTokenRefresh() {
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}

现在是注册类

public class RegistrationIntentService extends IntentService {
private static final String TAG = "REGISTRATION_SERVICE";

private int idClient;

public RegistrationIntentService() {
super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
this.idClient = intent.getIntExtra("idClient", 0);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

String locationProvider = LocationManager.NETWORK_PROVIDER;
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

double lat = 0;
double lon = 0;
if (lastKnownLocation != null) {
lat = lastKnownLocation.getLatitude();
lon = lastKnownLocation.getLongitude();
} else {
Log.d(TAG, "Position not available, updating next time.");
}

try {
synchronized (TAG) {
InstanceID instanceID = InstanceID.getInstance(this);
String senderId = getString(R.string.gcm_defaultSenderId);
String token = instanceID.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

this.sendRegistrationToServer(token, lat, lon);
}
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
}

Intent registrationComplete = new Intent(this, ActStartPage.class);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}

private void sendRegistrationToServer(String token, double lat, double lon) {
// doing some stuff
}
}

现在是监听器

public class PusherListenerService extends GcmListenerService {
private static final String TAG = "PUSHER_LISTENER_SERVICE";

@Override
public void onMessageReceived(String from, Bundle data) {
String title = data.getString("title");
String message = data.getString("body");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);

sendNotification(title, message);
}

private void sendNotification(String title, String message) {
Intent intent = new Intent(this, ActStartPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("menuTabId", 3);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent);

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

notificationManager.cancelAll();
notificationManager.notify((int)(Math.random()), notificationBuilder.build());
}
}

我的 list 是这样的

    <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" />
</intent-filter>
</receiver>
<service
android:name="clab.kalitys.mobile.CoreModuleManager.Pusher.PusherListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="clab.kalitys.mobile.CoreModuleManager.Pusher.PusherIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<service
android:name="clab.kalitys.mobile.CoreModuleManager.Pusher.RegistrationIntentService"
android:exported="false">
</service>

最后,我在 onCreate 的主要 Activity 中启动了所有这些

    if (checkPlayServices()) {
Intent intent = new Intent(this, RegistrationIntentService.class);
intent.putExtra("idClient", Integer.parseInt(this.getResources().getString(R.string.clientId)));
startService(intent);
}

那么现在,什么是有效的:

  • 启动注册:OK
  • 当应用程序处于前台或后台或从当前应用程序中删除时发送通知:确定
  • 点击通知并在应用程序处于前台时再次启动该应用程序:确定

什么不起作用 =( :

  • 当应用处于后台或从当前应用中移除时,点击通知并(再次)启动应用

我尝试了一些东西,但没有任何效果。我也不太了解 xml 设置。

这是我发送到 gcm 的 JSON 示例:

"notification":{
"body":"blabla",
"title":"one title"
"icon":"notification_icon"
},
"data":{
"body":"blabla",
"title":"one title"
},
"registration_ids":["id1", "id2", etc]

那么当我在应用程序关闭时收到通知时再次启动应用程序的代码(或我必须添加什么)有什么问题?

有什么想法吗?

谢谢 =)

最佳答案

好的,

我刚刚解决了这个问题。

我发送的 JSON 是这样的:

"notification":{
"body":"blabla",
"title":"one title"
"icon":"notification_icon"
},
"data":{
"body":"blabla",
"title":"one title"
},
"registration_ids":["id1", "id2", etc]

但这部分(来自 JSON):

"notification":{
"body":"blabla",
"title":"one title"
"icon":"notification_icon"
}

是问题所在。

我认为 JSONmessage 分为两部分,应用程序打开时的“数据”和应用程序关闭或在后台时的“通知”。我认为,因为应用程序已关闭,android 怎么知道他必须显示通知?所以这就是为什么使用“通知”来指定 android 必须显示通知。

但是不,“通知”用于发送简单的通知而不采取任何行动。

所以我通过发送这个 JSON 解决了​​这个问题:

"data":{
"body":"blabla",
"title":"one title"
},
"registration_ids":["id1", "id2", etc]

关于android - GCM 在收到通知时启动后台/关闭的应用程序 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36015687/

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