【问题标题】:Getting Same Firebase Notification multiple times多次获取相同的 Firebase 通知
【发布时间】:2018-05-29 07:50:26
【问题描述】:

我正在使用 firebase 来获取订阅主题的通知。 FirebaseMessagingService 多次收到相同的通知。我已经尝试了所有提供的解决方案,但对我没有任何帮助。任何帮助将不胜感激。提前致谢。

模块摇篮

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());
    }
}

【问题讨论】:

  • 您收到了多少次通知?
  • 喜欢 8 到 10 次。
  • 您使用什么服务来发送主题通知,例如价格更新
  • @VineshChauhan 我正在使用 curl 将通知从我的服务器发送到 firebase 服务器到特定主题。这样所有订阅该主题的设备都会收到该通知。
  • 你修好了吗?

标签: android firebase push-notification


【解决方案1】:

你能告诉我你的 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());
    }

【讨论】:

  • 我已经为你更新了我的代码块。现在我的 sendNotification() 暴露了。请看一看。谢谢
【解决方案2】:

中的第二个参数
PendingIntent contentIntent = PendingIntent.getActivity(
                getApplicationContext(),
                0,
                viewIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

被称为请求代码对于每个通知需要是唯一的

推荐https://developer.android.com/reference/android/app/PendingIntent.html

【讨论】:

  • 是的,我知道,但这只是为了单独显示通知。我担心的主要问题是为什么我的 onMessageReceived 会被多次调用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
  • 2020-02-05
  • 2016-01-26
  • 1970-01-01
相关资源
最近更新 更多