gpt4 book ai didi

android - 多次获取相同的 Firebase 通知

转载 作者:太空狗 更新时间:2023-10-29 14:41:21 25 4
gpt4 key购买 nike

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

模块 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
}

list

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

最佳答案

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

关于android - 多次获取相同的 Firebase 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47826631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com