2

當我收到'通知'GCM消息時,android會自動從標題和正文中創建一條通知,並將其放入通知欄。Android GCM「背景」通知在點擊時不會啓動我的應用程序

當我說「通知」在這裏,我的意思是本文檔中的一個叫通知https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages

我遇到的問題是,當我點擊的是,什麼也沒有發生 - 沒有啓動我的活動,或駁回通知。

我試過使用「click_action」選項,並且沒有任何效果。 我已經添加了一個「數據」部分,並沒有任何效果。

當我的應用程序在前臺時,我按預期得到了onMessageReceived,並且我構建的NotificationCompat根據預期重新啓動應用程序。

我錯過了什麼後臺通知來啓動應用程序?

這裏是清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    package="example.package" 
    android:versionCode="1" 
    android:versionName="1.0"> 

    <uses-sdk 
     android:minSdkVersion="16" 
     android:targetSdkVersion="21" /> 

    <!-- [START gcm_permission] --> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <!-- [END gcm_permission] --> 

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 

    <permission 
     android:name="example.package.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="example.package.permission.C2D_MESSAGE" /> 



    <application 
     android:name="example.package.application.appApplication" 
     android:allowBackup="true" 
     android:icon="@drawable/app_app_icon" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" 
     tools:replace="android:icon, allowBackup" 
     > 
     <activity 
      android:name="example.package.main.activity.MainActivity" 
      android:label="@string/app_name" 
      android:exported="true"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <!-- [START gcm_receiver] --> 
     <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" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
       <category android:name="example.package" /> 
      </intent-filter> 
     </receiver> 
     <!-- [END gcm_receiver] --> 


     <!-- [START gcm_listener] --> 
     <service 
      android:name="example.package.service.MyGcmListenerService" 
      android:exported="false"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      </intent-filter> 
     </service> 
     <!-- [END gcm_listener] --> 
     <!-- [START instanceId_listener] --> 
     <service 
      android:name="example.package.service.MyInstanceIDListenerService" 
      android:exported="false"> 
      <intent-filter> 
       <action android:name="com.google.android.gms.iid.InstanceID" /> 
      </intent-filter> 
     </service> 
     <!-- [END instanceId_listener] --> 
     <service 
      android:name="example.package.service.RegistrationIntentService" 
      android:exported="false"></service> 

     </application> 

</manifest> 

下面是一個簡單的JSON我送到GCM:

{ 
    "to" : "<snipped>", 
    "notification" : { 
     "title" : "<snipped>", 
     "body" : "<snipped>", 
     "icon" : "ic_launcher" 

    }, 
"data":{"a":"b"}, 
"content_available": true, 
"click_action":"android.intent.action.MAIN" 
} 

我,當我點擊通知窗口中的項目詳細日誌記錄得到的唯一的事是:

08-30 11:54:35.942 3684-3980/? D/InputReader: Input event(4): value=1 when=7306609326000 
08-30 11:54:35.942 3684-3980/? D/InputReader: Input event(4): value=1 when=7306609326000 
08-30 11:54:35.942 3684-3980/? I/InputReader: Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.323 ] when=7306609326000 
08-30 11:54:35.952 3684-3980/? D/InputReader: lastThreadEndTime = 7303509457022, currentThreadStartTime = 7303509466814, diff = 0 
08-30 11:54:35.952 3684-3979/? I/InputDispatcher: Delivering touch to (4091): action: 0x0, toolType: 1 
08-30 11:54:35.952 4091-4091/? D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN 
08-30 11:54:35.952 3684-4035/? D/lights: button : 1 + 
08-30 11:54:35.992 3684-4035/? D/lights: button : 1 - 
08-30 11:54:36.112 3684-3980/? D/InputReader: Input event(4): value=0 when=7306778958000 
08-30 11:54:36.112 3684-3980/? D/InputReader: Input event(4): value=0 when=7306778958000 
08-30 11:54:36.112 3684-3980/? I/InputReader: Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=] when=7306778958000 
08-30 11:54:36.112 3684-3979/? I/InputDispatcher: Delivering touch to (4091): action: 0x1, toolType: 1 

這是發生在棉花糖和軟糖裝置,雖然我沒有te在兩者之間進行任何事

我知道我可以單獨發送'data'元素,即使在後臺也可以啓動我的服務,但是如果應用程序已被強制停止,那麼它的缺點是無法工作。我真的想弄清楚我需要做些什麼來獲得谷歌在該鏈接上描述的行爲。

這裏是MyGcmListenerService - 當應用程序位於前臺時它確實得到正確調用。

public class MyGcmListenerService extends GcmListenerService { 

    private static final String TAG = "MyGcmListenerService"; 

    /** 
    * Called when message is received. 
    * 
    * @param from SenderID of the sender. 
    * @param data Data bundle containing message data as key/value pairs. 
    *    For Set of keys use data.keySet(). 
    */ 
    // [START receive_message] 
    @Override 
    public void onMessageReceived(String from, Bundle data) { 
//  String message = data.getString("message"); 
     String message = data.getBundle("notification").getString("body"); 
     String customFields = data.getBundle("notification").getString("customFields"); 
     String title = data.getBundle("notification").getString("title"); 
     Log.d("From: " + from); 
     Log.d("Title: " + title); 
     Log.d("Message: " + message); 
     Log.d("CustomFields: " + customFields); 


     if (from.startsWith("/topics/")) { 
      // message received from some topic. 
     } else { 
      // normal downstream message. 
     } 

     // [START_EXCLUDE] 
     /** 
     * Production applications would usually process the message here. 
     * Eg: - Syncing with server. 
     *  - Store message in local database. 
     *  - Update UI. 
     */ 

     /** 
     * In some cases it may be useful to show a notification indicating to the user 
     * that a message was received. 
     */ 
     sendNotification(message); 
     // [END_EXCLUDE] 
    } 
    // [END receive_message] 

    /** 
    * Create and show a simple notification containing the received GCM message. 
    * 
    * @param message GCM message received. 
    */ 
    private void sendNotification(String message) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.setAction(Intent.ACTION_MAIN); 
     intent.addCategory(Intent.CATEGORY_LAUNCHER); 
     /* resumes the current activity - just like pressing link on home page */ 


     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_stat_ic_notification) 
       .setContentTitle("GCM Message") 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 
} 

要澄清,我如何得到這個行爲與GCM合作尋找具體爲:

根據https://developers.google.com/cloud-messaging/downstream「當你的應用程序是在後臺,Android的指導信息,併發出通知到系統托盤。用戶點擊通知會默認打開應用啓動器。'

我已經能夠用FCM重新編寫它,但我沒有看到這個問題,但我真的想知道我錯過了什麼。

+0

MyGcmListenerService.java的郵編 – Bhupendra

+0

@Bhupendra我會發布它,但它與googles git頁面上的gcm相同。 在此過程中,當通知欄中的通知由操作系統創建時,它不會以任何方式調用。 –

回答

0

基於此related SO question,在JSON中,「通知」用於發送無操作的簡單通知。

根據該documentation

通知提供供開發者發送一個用戶可見的顯示消息用一些預定義的鍵和可選的自定義鍵/值對的簡單方法。數據有效載荷僅包含開發人員的自定義鍵/值對,而客戶端應用程序必須處理該消息。您可以發送既有通知又有數據有效載荷的消息。

這個SO question也建議用消息接收器中的MainActivity.class替換入口活動。

final Intent notificationIntent = new Intent(context, MainActivity.class); 
notificationIntent.setAction(Intent.ACTION_MAIN); 
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

您還可以檢查此link關於如何被點擊通知時,打開應用程序的示例代碼。希望這可以幫助!

+0

感謝您的信息,但它不應該是沒有行動,它應該打開發射器的意圖。你的第二部分顯示了java代碼,看起來像你在推薦我使用「數據」消息,這正是我試圖避免的。根據https://developers.google.com/cloud-messaging/downstream'當您的應用程序處於後臺時,Android會將消息通知系統托盤。用戶點擊通知會默認打開應用啓動器 –

相關問題