没有收到背景和内容可用= 1的应用的GCM通知 [英] Not receiving GCM notifications with app in background and content-available=1

查看:196
本文介绍了没有收到背景和内容可用= 1的应用的GCM通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个Ionic 2应用程序并在Android模拟器上测试。



当应用程序处于后台并且通知没有标题时,没有消息和content-available = 1通知应直接发送到应用程序通知处理程序。但它没有发生。



我可以在前台收到有关应用程序的通知。



如果我有一个标题以及我在通知区域收到通知的消息。但是我需要以静默方式直接发送通知给应用程序,而不需要通过通知区域。



这是我发送推送通知的代码:


$ b $

  {
delay_while_idle:true,
priority:high,
sound :
color:FFFF00,
//有效负载
数据:{
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>
< / intent-filter>
< / service>
< service
android:name =。gcm.TokenRefreshService
android:exported =false>
< intent-filter>
< / 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:重命名操作,选择操作名称描述这个
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);
}
}


I developing an Ionic 2 app and testing on Android emulator.

When the app is in background and the notification has no title, no message and content-available=1 the notification should be sent directly to app notification handler. But its not happening.

I can receive notifications with the app in foreground.

If I have a title and a message I receive the notification in the notification area. But I need to send the notification directly to the app in silent mode, without pass by the notification area.

Here is my code to send push notifications:

{
   "delay_while_idle": true,
   "priority": "high",
   "sound": "default",
   "color": "FFFF00",
   //payload   
   "data": {
       "content-available": "1",
       "some_var": "some_value",
       "ohter_var": "other_value",       
   }
}

How could I sent silent notifications to my Android app?

解决方案

Android GCM and FCM both also work when app background.

For that you need to add below service classes at manifest with intent Filter.

<!-- [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);
    }
}

To get token:

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);
}
}

这篇关于没有收到背景和内容可用= 1的应用的GCM通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆