2016-08-10 19 views
0

私はアンドロイドアプリでGCMサービスを実装していますが、私のアプリが閉鎖されているかバックグラウンドになっているときにも通知が届いています。アプリがAndroidにバックグラウンドである場合、通知アイコンが表示されない(Gcmプッシュ通知)?

アプリがフォアグラウンドになるとすべて正常に動作していますが、すべてのテキストとアイコンで通知されていますが、アプリがバックグラウンドのときに通知テキストとタイトルが表示されますが、アイコンは表示されません。私はこれについて調査し、あなたのアプリがバックグラウンドであるときにデバイス通知トレイによって通知が処理されるという結論に達しました。ここで

は、通知を受け取るために私のコードです:

public class GCMPushReceiverService extends GcmListenerService { 

//This method will be called on every new message received 
@Override 
public void onMessageReceived(String from, Bundle data) { 
    //Getting the message from the bundle 
    String message = data.getString("message"); 
    Log.d("data",data.toString()); 
    //Displaying a notiffication with the message 
    String body = null; 
    String title = null; 
    try{ 

     String data1 = data.toString(); 
     String json = (data1.split("notification=Bundle\\[")[1]).split("\\]")[0]; 

     body = (json.split("body\\=")[1]).split("\\,")[0]; 
     // title = (((json.split("body\\=")[1]).split("\\,")[1]).split("title\\=")[1]).split("\\,")[0]; 
     title = (((json.split("body\\=")[1]).split("vibrate")[0]).split("title=")[1]).split(",")[0]; 



     Log.d("json",json); 

     JSONObject notificationJSON = new JSONObject(json); 

     //String notificationJSONString = data.getString("notification"); 
     //then you can parse the notificationJSONString into a JSON object 
     // JSONObject notificationJSON = new JSONObject(notificationJSONString); 
     // body = notificationJSON.getString("body"); 
     //title = notificationJSON.getString("title"); 

     Log.d("body",body); 
     Log.d("title",title); 


    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
    // sendNotification(message); 
    sendNotification(body, title); 
} 

//This method is generating a notification and displaying the notification 
private void sendNotification(String message,String titles) { 
    Intent intent = new Intent(this, NavigationDrawerActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.putExtra("firsttab","notify"); 
    int requestCode = 0; 
    int number = 0; 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT); 
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 
    NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this) 
      // .setSmallIcon(R.mipmap.philips_launcher) 
      .setSmallIcon(getNotificationIcon()) 
      .setContentTitle(titles) 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(sound) 
      .setNumber(++number) 
      .setColor(Color.parseColor("#0089C4")) 
      // .setStyle(inboxStyle) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(titles)) 

      .setContentIntent(pendingIntent); 
    /* if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     builder.setSmallIcon(R.drawable.icon_transperent); 
    } else { 
     builder.setSmallIcon(R.drawable.icon); 
    }*/ 



    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0, noBuilder.build()); //0 = ID of notification 
} 

private int getNotificationIcon() { 
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP); 
    return useWhiteIcon ? R.drawable.notification_icon : R.drawable.not_icon; 
} 



} 

私の質問は、私のアプリがバックグラウンドで動作しているときに通知を扱いする方法ですか?また、アプリがバックグラウンドのときに通知アイコンを表示する方法は?通知をクリックすると、launcherActivityが開きますが、otherActivityを開きます。

+0

これを試してみましたか? http://stackoverflow.com/a/29207365/3092341 – lal

+0

@lal:これを試して透明アイコンを使用しましたが、自分のアプリがバックグラウンドのときにGCMPushReceiverServiceが呼び出されないという問題があると思いますか? – Anand

+0

あなたはそれが呼ばれていないと確信していますか? –

答えて

1

このthreadに基づいて、アプリがバックグラウンドにある場合、アクションを実行するにはサーバー側が必要です。データ部分は自動的にインテントに保存され、コンテンツフィルタにそのアクションが含まれているアクティビティに送信されます。このrelated SO questionには、ダウンストリームメッセージ要求の「通知」オブジェクトで渡されたプロパティに基づいて通知メッセージが自動的に通知を生成すると記載されていますが、この場合はonMessageReceivedは呼び出されません。これをチェックしてくださいtutorial。バックグラウンドでは、アプリは通知トレイに通知ペイロードを受け取り、ユーザーが通知をタップするとデータペイロードのみを処理します。

また、メッセージの送信時にはset the priority to highとすることもできます。可能であれば、GCMサービスはスリープ状態のデバイスを復帰させ、アプリケーションサーバーとのネットワーク接続を開きます。あなたはこれらの関連リンクをチェックすることができ

は、この情報がお役に立てば幸い!

0

通知メッセージを送信しているように見えます。通知メッセージは、あなたが見ているようにアプリケーションがフォアグラウンドにあるときにonMessageReceivedコールバックに配信されます。しかし、アプリケーションがフォアグラウンドの通知メッセージに入っても、onMessageReceivedには渡されません。メッセージの通知ペイロードは、通知を自動的に表示するために使用されます。付随するデータペイロードがある場合、ユーザーが通知をタップしたときに、起動されたインテントの追加でペイロードが利用可能になります。

メッセージの処理方法を完全に制御したい場合は、常にonMessageReceivedコールバックに配信されるデータメッセージを使用する必要があります。

types of FCM messagesの2つの異なる方法については、こちらをご覧ください。

注: - Firebaseコンソールを使用してメッセージを送信している場合は、現在のところ通知メッセージのみがサポートされています。カスタムデータを追加しても、依然としてクライアント側で通知メッセージとして扱われます。 - REST APIを使用して通知メッセージを送信する場合は、click_actionフィールドを指定して、ユーザーが通知をクリックしたときに起動されるアクティビティを特定できます。

+0

、ありがとうございます。私はデータペイロードを追加しようとしています。 – Anand

0

あなたはアプリのマニフェストでこれを追加、FCMを使用している場合:アプリがバックグラウンドで動作しているとき

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. --> 
<meta-data 
    android:name="com.google.firebase.messaging.default_notification_icon" 
    android:resource="@drawable/ic_stat_ic_notification" /> 
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming 
    notification message. --> 
<meta-data 
    android:name="com.google.firebase.messaging.default_notification_color" 
    android:resource="@color/colorAccent" /> 

のみ、カスタムカラー&アイコンが通知に表示されます。

関連する問題