对不起我的英语。在工作之前一切正常。但是在我不知道我同时有 2 个推送之后,看起来我有 2 个线程,每个线程都创建自己的通知。我检查,服务器向我发送了 1 个通知,但 Android 设备创建了 2 个通知。为什么?我花了很多时间,但我无法解决这个问题。有知道的请指教
应用程序等级
compile 'com.google.firebase:firebase-messaging:10.0.1'
compile 'com.google.firebase:firebase-core:10.0.1'
}
apply plugin: 'com.google.gms.google-services'
渐变
classpath 'com.google.gms:google-services:3.0.0'
list
<receiver
android:name="push.GcmBroadcastReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.nga" />
</intent-filter>
</receiver>
<service
android:name="push.FBMnotification"
android:exported="false"
android:process=":remote">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
GcmBroadcastReceiver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("GcmBroadcastReceiver", "GcmBroadcastReceiver");
ComponentName comp = new ComponentName(context.getPackageName(),
FBMnotification.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
FBM通知
public class FBMnotification extends IntentService {
public FBMnotification() {
super("firebase");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle data = intent.getExtras();
//create push
}
}
我无法扩展 FirebaseMessagingService,因为我无法在应用程序关闭时创建自定义通知。 FirebaseMessagingService 创建自己的通知而不需要我的字段和 Intent
最佳答案
你有一个 Receiver
和一个 Service
采取行动 <action android:name="com.google.android.c2dm.intent.RECEIVE" />
.我不确定,但我认为这就是原因。看看我自己的项目中的示例,它可以正常工作。
AndroidManifest.xml
<service
android:name=".service.NotificationsListenerService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
我只有这个Service
接收所有通知。
public class NotificationsListenerService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage message) {
sendNotification("You have a new order to release");
}
private void sendNotification(String message) {
String _jsonUser = SharedPreferencesUtils.getString(this, SharedPreferencesUtils.Constants.LOGGED_USER_KEY);
Intent _intent;
if (_jsonUser != null) {
UserVO _user = new Gson().fromJson(_jsonUser, UserVO.class);
Session.putObject(Session.Key.USER, _user);
_intent = new Intent(this, OrderListActivity.class);
}
else {
_intent = new Intent(this, LoginActivity.class);
}
_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent _pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, _intent, PendingIntent.FLAG_ONE_SHOT);
Uri _sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder _notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(message)
.setAutoCancel(true)
.setSound(_sound)
.setContentIntent(_pendingIntent);
NotificationManager _notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
_notificationManager.notify(0 /* ID of notification */, _notificationBuilder.build());
}
}
关于android - Firebase 创建推送两次 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41850874/