gpt4 book ai didi

android - onMessageReceived() 在应用程序处于前台时不被调用

转载 作者:搜寻专家 更新时间:2023-11-01 07:48:08 26 4
gpt4 key购买 nike

我正在使用以下代码接收通过 firebase 控制台发送的 fcm 消息,但从未调用此函数。相反,当应用程序处于后台时,我能够在我的启动器类中接收消息,但是当应用程序处于前台时,我无法获取它。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";
String message="";
Map<String, String> m1;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
System.out.println("---------------------");

message = remoteMessage.getNotification().getTitle();
m1= remoteMessage.getData();
System.out.println("=============="+m1);
System.out.println("=============="+m1.get("url"));

//Calling method to generate notification
//sendNotification(remoteMessage.getNotification().getBody());
//sendNotification_test(message);
createNotification(m1,remoteMessage.getNotification().getBody());
}


private void createNotification(Map<String,String> payload, String title){

Intent intent = new Intent(this, LoginActivity.class);


intent.putExtra("url",payload.get("url"));
intent.putExtra("package",payload.get("package"));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("FCM Message")
.setContentText(title)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}

list

 <service
android:name="com.example.pranshusrivastav.fcmtest.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<service
android:name="com.example.pranshusrivastav.fcmtest.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>

最佳答案

我将服务声明放在 list 中的应用程序标签之外:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".LoginActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name="com.example.pranshusrivastav.fcmtest.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<service
android:name="com.example.pranshusrivastav.fcmtest.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>

这成功了!!

关于android - onMessageReceived() 在应用程序处于前台时不被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39641044/

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