未收到背景和内容可用的应用程序GCM通知= 1
问题描述:
我开发了一个Ionic 2应用程序并在Android模拟器上测试。未收到背景和内容可用的应用程序GCM通知= 1
当应用程序在后台并且通知没有标题,没有消息和content-available = 1时,通知应直接发送到应用程序通知处理程序。但没有发生。
我可以在前台收到与应用程序通知。
如果我有一个标题和一条消息,我会在通知区域收到通知。但是我需要以静默方式直接将通知发送到应用程序,而不通过通知区域。
这里是我的代码发送推送通知:
{
"delay_while_idle": true,
"priority": "high",
"sound": "default",
"color": "FFFF00",
//payload
"data": {
"content-available": "1",
"some_var": "some_value",
"ohter_var": "other_value",
}
}
我怎么能发送无声通知我的Android应用?
答
Android GCM和FCM都可以在应用程序背景下工作。
为此,您需要在意向过滤器的清单中添加以下服务类。
<!-- [START gcm_listener] -->
<service
android:name=".gcm.GcmListener"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name=".gcm.TokenRefreshService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name=".gcm.RegistrationIntentService"
android:exported="false" />
<!-- [END gcm_listener] -->
public class GcmListener extends GcmListenerService {
}
public class TokenRefreshService extends InstanceIDListenerService {
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
要获得令牌:
public class RegistrationIntentService extends IntentService {
// TODO: Rename actions, choose action names that describe tasks that this
private String TAG = "RegistrationIntentService";
public RegistrationIntentService() {
super("RegistrationIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken("PRODUCT_ID",
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
}
}
感谢您的回复,但正如我说这是一个离子2应用程序。我不直接处理Java代码或Android XML。此外,我不认为这是一个前端配置问题,但一个GCM消息配置问题。 – Natanael