【问题标题】:Not receiving GCM notifications with app in background and content-available=1在后台和 content-available=1 中未收到应用程序的 GCM 通知
【发布时间】:2016-07-28 21:55:37
【问题描述】:

我正在开发一个 Ionic 2 应用并在 Android 模拟器上进行测试。

当应用在后台并且通知没有标题、没有消息并且 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 push-notification google-cloud-messaging background-process ionic2


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复,但正如我所说,这是一个 Ionic 2 应用程序。我不直接处理 Java 代码或 Android XML。此外,我认为这不是前端配置问题,而是 GCM 消息配置问题。
    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 2014-05-21
    相关资源
    最近更新 更多