2017-12-15 3 views
1

firebaseを使用して、購読トピックの通知を取得しています。 FirebaseMessagingServiceは同じ通知を複数回受信します。私は提供されたすべてのソリューションを試しましたが、何も私のために働いた。どんな助けも高く評価されます。前もって感謝します。同じFirebase通知を複数回取得する

モジュールのGradle

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "24.0.3" 

    defaultConfig { 
     applicationId "abc.abc" 
     minSdkVersion 19 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:25.3.1' 
    compile 'com.squareup.retrofit:retrofit:1.9.0' 
    compile 'com.squareup.okhttp:okhttp:2.4.0' 
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0' 
    compile 'com.github.bumptech.glide:glide:3.5.2' 
    compile 'com.wang.avi:library:2.1.3' 
    compile 'com.google.firebase:firebase-messaging:11.0.4' 
    compile 'com.facebook.fresco:fresco:1.5.0' 

} 
apply plugin: 'com.google.gms.google-services' 

プロジェクトのGradle

sub-projects/modules. 

buildscript { 
    repositories { 

     jcenter() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:2.1.0' 
     classpath 'com.google.gms:google-services:3.1.0' 

    } 
} 

allprojects { 
    repositories { 
     jcenter() 

    } 
} 

task clean(type: Delete) { 
    delete rootProject.buildDir 
} 

マニフェスト

<service android:name=".Firebase.MyFirebaseMessagingService" > 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
      </intent-filter> 
     </service> 
     <service android:name=".Firebase.MyFirebaseInstanceIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
      </intent-filter> 
     </service> 

FirebaseMessagingService この通知関数は、単一の通知を送信しているにも関わらず、複数回呼び出されます。

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     //Displaying data in log 
     //It is optional 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
     Log.d(TAG,refreshedToken); 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 

     //Calling method to generate notification 
     sendNotification(remoteMessage.getNotification().getBody()); 
    } 

private void sendNotification(String messageBody) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.bazaar_noti_small) 
       .setContentTitle("Price Updated") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0, notificationBuilder.build()); 
    } 
} 
+0

のために一意である必要がありますか? –

+0

は8〜10回を好む。 –

+0

トピックのような通知を送るためにどのサービスを使用していますか更新されました –

答えて

0

あなたは私にあなたのsendNotificationを()機能を表示することができます。

以下のコードは、私のサンプルプッシュ通知です。私は

private void sendNotification(String title,String messageBody) 
    { 
     Intent viewIntent = new Intent(getApplicationContext(), MainActivity.class); 

     PendingIntent contentIntent = PendingIntent.getActivity(
       getApplicationContext(), 
       0, 
       viewIntent, // add this 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(getNotificationIcon()) 
       .setTicker("Test App") 
       .setContentTitle(title) 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(contentIntent); 

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

     //Random r = new Random(); 
     //int randomNumber = r.nextInt(100 - 0) + 0; 

     notificationManager.notify(1, notificationBuilder.build()); 
    } 
+0

自分のコードブロックを更新しました。今私のsendNotification()が公開されています。見てください。ありがとう –

0

PendingIntent contentIntent = PendingIntent.getActivity(
       getApplicationContext(), 
       0, 
       viewIntent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

の2番目のパラメータは、要求コードとして知られているが、あなたは通知を持って何回各通知

REFERE https://developer.android.com/reference/android/app/PendingIntent.html

+0

はい、私はそれを知っていますが、それは通知を別々に表示することだけです。私が心配している主な問題は、なぜ私のonMessageReceivedが複数回呼び出されたかということです。 –

関連する問題