gpt4 book ai didi

java - Firebase Messaging 仅适用于单独的设备

转载 作者:行者123 更新时间:2023-12-02 09:05:11 24 4
gpt4 key购买 nike

我目前正在为我的应用程序编写一个功能,允许用户向员工提出请求,以便他们可以接受或拒绝工作机会。我正在使用 firebase 消息系统。当我有两部手机时,通知有效,工作人员可以接受或拒绝,如下所述:

public class MyFirebaseMessaging extends FirebaseMessagingService {
private static final String COLE_CHANNEL_ID = "com.example.usub.COLE";
@Override
public void onMessageReceived(RemoteMessage remoteMessage){
LatLng customer_location = new Gson().fromJson(remoteMessage.getNotification().getBody(),LatLng.class);
Intent intent = new Intent(getBaseContext(), CustomerCall.class);


intent.putExtra("lat", customer_location.latitude);
intent.putExtra("lng", customer_location.longitude);
intent.putExtra("customer", remoteMessage.getNotification().getTitle());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}

但是,我尝试在手机上运行涉及这两个应用程序的演示,我收到已发出请求的通知,但是当单击通知时,应用程序将打开,但不会重新收集数据。我的通知助手如下所示:

public class NotificationHelper extends ContextWrapper {

private static final String COLE_CHANNEL_ID = "com.example.usub.COLE";
private static final String COLE_CHANNEL_NAME = "STRADTMANNSOLUTIONS Usub";

private NotificationManager manager;

public NotificationHelper(Context base) {
super(base);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
createChannels();
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannels() {
NotificationChannel coleChannels = new NotificationChannel(COLE_CHANNEL_ID,
COLE_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT);
coleChannels.enableLights(true);
coleChannels.enableVibration(true);
coleChannels.setLightColor(Color.GRAY);
coleChannels.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

getManager().createNotificationChannel(coleChannels);
}

public NotificationManager getManager() {
if(manager == null)
manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
return manager;
}
@RequiresApi(api = Build.VERSION_CODES.O)
public Notification.Builder getUsubNotification(String title, String content, PendingIntent contentIntent,
Uri soundUri)
{
return new Notification.Builder(getApplicationContext(),COLE_CHANNEL_ID)
.setContentText(content)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_notify);
}
}

有谁知道为什么通知会丢失所有数据,或者我如何使用按下通知后的新信息打开应用程序以进行特定 Activity ?谢谢

最佳答案

仅当您的应用位于前台时,通知消息才会传递到您的 onMessageReceived 回调。如果应用程序位于后台,则会显示通知,但不会触发 onMessageRecieved,并且该消息中的数据将传递到启动的 Intent 。你可以使用:-

getIntent().getExtras();

检索通过通知发送的数据。

查看文档 this

关于java - Firebase Messaging 仅适用于单独的设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59872254/

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