Я реализовал передачу облачных сообщений в базе данных Firebase в своем приложении для Android. Когда я отправляю уведомление с резервной или 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"}, тип = обмен , title = Тестовый пользователь: 1556703533, message = Тестовый пользователь1}
2019-05-01 15: 08: 58.542 29417-29501 / in.example.one E / NOTIF: {extras = {"customerId": "5e341186-6bd4-11e9-9069-44a8422a303b"}, тип = обмен , title = Тестовый пользователь: 1556703533, message = Тестовый пользователь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;