我正在尝试熟悉Firebase通知.它工作正常,但我没有在应用程序未打开时从通知控制台接收消息.
我知道文档说:
如果您的应用程序在前台或后台,您可以在onMessageReceived方法中接收消息,否则用户将收到托盘中的通知...单击它将打开主要活动与意图内的数据
但即使应用程序关闭,有没有办法从通知控制台捕获每条消息?
==== 答案 ====
在这里找到答案
无法从通知控制台发送数据消息.
但是还有其他方法可以向设备发送通知,它们将被捕获在onMessageReceived中!
您可以使用终端(Mac或Linux)或Postman等服务在此链接上发送Post请求:https://fcm.googleapis.com/fcm/send
与下一个身体:
{
"to": "/topics/your_topic_here",
"data": {
"text":"text",
"text1":"text1",
...
}
}
Run Code Online (Sandbox Code Playgroud)
你还需要添加2个标题:
要获取服务器密钥,可以在firebase控制台中找到它:您的项目 - >设置 - >项目设置 - >云消息 - >服务器密钥
android firebase firebase-cloud-messaging firebase-notifications
我想知道每当Firebase在特定实体上添加新的孩子时,是否可以向Android移动设备发送推送通知.例如,假设Firebase上有一个名为Tasks的实体.每当向该firebase集合添加新任务时,都会触发"child_added"事件,然后以某种方式将推送通知发送到移动客户端.触发器是child_added事件.但是,我不确定是否可以直接从Firebase事件发送推送通知.
我将firebase通知集成到了我的应用程序中,但我想发送一个通知,打开一个特定的活动,按照我的计划做,而不仅仅是打开应用程序.比如通知会在用户点击它时访问Google Play商店.
我看到了代码Firebase控制台:如何为我使用的通知指定click_action但是初始化变量cls时出错.我尝试通过定义cls = null来解决,以清除错误.它无法使用click_action打开我指定的活动
public class ClickActionHelper {
public static void startActivity(String className, Bundle extras, Context context){
Class cls=null;
try { cls = Class.forName(className);
}
catch(ClassNotFoundException e){
//means you made a wrong input in firebase console
}
Intent i = new Intent(context, cls);
i.putExtras(extras); context.startActivity(i);
}
}
Run Code Online (Sandbox Code Playgroud)
我有什么问题吗?我如何让它工作?
EDIT3:好的,我发现了问题:如果应用程序被杀死,则同时发送data 和 发送都notification不会触发onMessageReceived。notification如果目标设备是android,则需要将其设置为null。
这确实是一种愚蠢的行为。
原始帖子:
notificationBuilder启动应用程序后,我可以成功地从传递给活动的意图中检索捆绑包。
但是,如果我终止了该应用程序,则通知仍然有效,但GetExtras()意图上的则为null。
在我的FirebaseMessagingService孩子班上:
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("key_1","value");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//also tried with PendingIntent.FLAG_CANCEL_CURRENT
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("title")
.setContentText("messageBody")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Run Code Online (Sandbox Code Playgroud)
当应用被杀死时,firebaseMessagingService甚至无法写入文件。它不会崩溃,它会生成并显示通知,但是对磁盘所做的任何操作均不起作用。
编辑:我已替换onMessageReceived方法的内容,如下所示:
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{ …Run Code Online (Sandbox Code Playgroud)