0

我開發了一個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應用?

回答

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

感謝您的回覆,但正如我說這是一個離子2應用程序。我不直接處理Java代碼或Android XML。此外,我不認爲這是一個前端配置問題,但一個GCM消息配置問題。 – Natanael

相關問題