2

「通知」GCMメッセージを受信すると、アンドロイドは自動的にタイトルと本文から通知を作成し、通知シェードに入れます。私はここで、私はこの文書の1と呼ばれる通知を意味し、「通知」を言うAndroid GCM「背景」通知がクリックされたときにアプリを起動しない

https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages

私が午前問題は、私はそれをクリックすると、何も起こらないということである - それは私の活動を起動するか、しません通知を閉じます。

"click_action"オプションを使用しようとしましたが、それは効果がありませんでした。 "data"セクションを追加しましたが、それは効果がありません。

私のアプリケーションがフォアグラウンドになっているとき、私は予想どおりにonMessageReceivedを取得し、それをビルドしたNotificationCompatはアプリケーションを期待どおりに再起動します。

アプリを起動するための背景通知を受け取るために私は何が欠けていますか?ここで

は、私がGCMに送信サンプルJSONここでマニフェスト

<?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> 

されている。

{ 
    "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 

これはMarshmallowとJellyBeanデバイスで発生していますが、間に何かを刺す。

バックグラウンドでも私のサービスを蹴るために 'data'要素だけを送ることができますが、アプリケーションが強制停止されていればそれは機能しないという欠点があります。私は実際にGoogleがそのリンクで説明している行動を得るために必要なことを理解しようとしています。

ここは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私はそれを投稿しますが、googleと同じgcmのgitページと同じです。 このプロセス中に、通知バーの通知がOSによって作成されたときに、どのような方法でも呼び出されているようには見えません。 –

答えて

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

情報ありがとうございますが、アクションなしではいけないとは限りません。ランチャーの意図を開くはずです。あなたの2番目の部分は、私が避けようとしている「データ」メッセージを使用することを推薦しているような、Javaコードを示しています。 https://developers.google.com/cloud-messaging/downstream'によると、アプリがバックグラウンドにあるとき、Androidはシステムトレイに通知されたメッセージを送信します。ユーザーが通知をクリックすると、デフォルトでアプリランチャーが開きます。 –

関連する問題