两次调用了Android FirebaseMessaging服务onMessageReceived

时间:2019-05-01 09:54:35

标签: android firebase firebase-cloud-messaging firebase-console

我在Android应用中实现了Firebase云消息传递。当我从备份或Firebase控制台发送通知时,onMessageReceived()被触发两次并在设备上生成两个通知。我尝试在互联网上搜索,但未找到此问题的结果

这是我的代码,

MyFirebaseNotificationService.java

public class MyFirebaseNotificationService extends FirebaseMessagingService {
    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);

        MyApp.getInstance().saveFCMToken(s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        int notificationId = new Random().nextInt(60000);

        String customerId = "";
        Log.e("NOTIF", "" + remoteMessage.getData());


        Intent notificationIntent = new Intent(this, SplashActivity.class);

        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        notificationIntent.setAction(Long.toString(System.currentTimeMillis()));
        final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "100")
                .setSmallIcon(R.drawable.ic_app_icon)
                .setColorized(true)
                .setPriority(PRIORITY_HIGH)
                .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
                .setContentTitle(Html.fromHtml(remoteMessage.getData().get("title")))
                .setContentText(Html.fromHtml(remoteMessage.getData().get("message")))
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        createNotificationChannel();
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(notificationId, notificationBuilder.build());

    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Exchange Customer";
            String description = "Sales Buddy";
            String CHANNEL_ID = "100";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            getSystemService(NotificationManager.class).createNotificationChannel(channel);
        }
    }
}

AndroidManifest

  <service android:name=".sevices.MyFirebaseNotificationService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

清单中的权限

 <uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />

收到消息后,我已记录通知,这里是logcat

  

2019-05-01 15:08:54.415 29417-29501 / in.example.one E / NOTIF:{extras = {“ customerId”:“ 5e341186-6bd4-11e9-9069-44a8422a303b”},类型=交换,标题=测试用户:1556703533,消息=测试用户1}

     

2019-05-01 15:08:58.542 29417-29501 / in.example.one E / NOTIF:{extras = {“ customerId”:“ 5e341186-6bd4-11e9-9069-44a8422a303b”},类型=交换,标题=测试用户:1556703533,消息=测试用户1}

在这里您可以看到相同的通知日志正在打印两次,并且两个通知都在设备上显示

项目Gradle文件

  dependencies {
    classpath 'com.android.tools.build:gradle:3.4.0'
    classpath 'com.google.gms:google-services:4.2.0'
    classpath 'io.fabric.tools:gradle:1.26.1'

}

模块化Gradle文件

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.amitshekhar.android:android-networking:1.0.2'
implementation 'com.github.WindSekirun:SectionCalendarView:1.0.5.1'
implementation 'com.github.darsh2:MultipleImageSelect:v0.0.4'
implementation 'com.bogdwellers:pinchtozoom:0.1'

implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-messaging:17.6.0'
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
implementation 'com.google.firebase:firebase-database:16.1.0'

}

我的PHP代码

$extras= json_encode(['customerId' => "5e341186-6bd4-11e9-9069-44a8422a303b"]);
$data=array(
    'title'=> "Test User:".time(),
    'message'=> "Test User1",
    'type'=> "exchange",
    'extras'=>$extras   
);
$notification=array(
    'title'=> "Test User:".time(),
    'body'=> "body1",
);
$fields = array
    (
        'to'=>'/topics/test-exchange-persons-sales-buddy',
        'data'  => $data
    );


$headers = array
        (
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        );

    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
    curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
    $result = curl_exec($ch );
    curl_close( $ch );

    echo $result;

3 个答案:

答案 0 :(得分:0)

我有相同的问题,但使用Firebase邮件主题。我收到了两次像您一样两次被称为“ onMessageReceived”的通知通知。也许今天来自FCM的问题?

答案 1 :(得分:0)

自昨天以来,我遇到了同样的问题(也使用主题)。作为解决方法,在修复之前,请在我的FirebaseMessagingService中进行此操作:

private static ArrayList<Long> alreadyNotifiedTimestamps = new ArrayList<>();

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (!isDuplicate(remoteMessage.getSentTime())) {
        // send notificaiton here
    }
}

// Workaround for Firebase duplicate pushes
private boolean isDuplicate(long timestamp) {
    if (alreadyNotifiedTimestamps.contains(timestamp)) {
        alreadyNotifiedTimestamps.remove(timestamp);
        return true;
    } else {
        alreadyNotifiedTimestamps.add(timestamp);
    }

    return false;
}

答案 2 :(得分:0)

我也遇到过类似的问题,由于implementation 'com.google.firebase:firebase-messaging:17.6.0',所以我只使用了implementation 'com.google.firebase:firebase-messaging:17.3.3'版的消息传递,所以一切正常