- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 firebase-cloud-messaging
将通知推送到我的应用程序。我希望当用户单击通知时,它会打开 OpenWeb 类和 Intent 打开 url。
$data = array("notification"=> array("title"=>"http://example.com/",
"text"=>"This is a test msg!",
"click_action"=>"OpenWeb"),
"priority" => "high",
"to"=>"/topics/main");
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
String click_action = remoteMessage.getNotification().getClickAction();
Context context = getApplicationContext();
Intent intent = new Intent(click_action);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("link",title);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 , intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.notification))
.setContentTitle("Notification")
.setContentText(body)
.setSound(defaultSoundUri)
.setAutoCancel(true)
.setContentIntent(contentIntent);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent.getStringExtra("link"));
startActivity(intent);
}
<activity android:name=".OpenWeb">
<intent-filter>
<action android:name="OpenWeb" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
当我单击通知时, Activity 启动,OpenWeb
中的 getIntent.getStringExtra("link")
为 null。这意味着 MyFirebaseMessagingService 类不会将数据发送到 Activity。
如何在 OpenWeb
上发送数据?
最佳答案
代码有一些问题1. 在您的 php 脚本中,您没有指定任何 "body"
但您仍在调用 String body = remoteMessage.getNotification().getBody();
那是一个空指针异常。
2.此 click_action 键仅在应用程序处于前台时才起作用,对于应用程序处于后台并关闭时的所有其他情况,它不起作用。
3.您需要设置标志来启动新 Activity ,因此 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
因此,如果待处理的意图正在触发,由于第 2 个原因,您最终会得到一个 npe,现在您可以通过发送数据消息而不是处理您的通知的 firebase sdk 来解决此问题,然后它将是 NONNULL
好吧,firebase 有两种类型的消息
data masseages has to be key-value pairs though.
举个例子,您想要发送一些数据以及客户端应用程序将处理的通知,例如您要发送一些有关体育比赛的新闻,这样您就可以创建一个 JSON 格式的节点
data: {
coolNews: "Some Cool News1",
coolNews2: "Some really cool news2"
}
然后你可以调用remoteMessage.getData().get("coolNews");
但是要小心,因为如果您发送以下 JSON
Notification:{
to: //UserToken ,
title: "Some Title",
body: "Some body",
data: {
coolNews: "Some Cool News1",
coolNews2: "Some really cool news2"
}}
只有在客户端打开通知后才会管理数据负载,但是如果您将整个通知 json 作为数据负载发送并在客户端处理所有内容,那么所有内容都会同时接收。像下面这样:
Notification:{
to: //UserToken
data: {
title: "Some Title",
body: "Some body",
coolNews: "Some Cool News1",
coolNews2: "Some really cool news2"
}}
然后在您的 FirebaseMessageId
中,您可以调用 remotemessage.getData().get("title");
来获取标题,对于所有其他内容也同样如此,例如body
和 coolNews
..
现在,对于 BroadCast 接收者
,您可以通过 firebasemessagingId
发送广播,如下所示:
Intent intent = new Intent(this, NotificationBR.class);
Bundle bundle = new Bundle();
bundle.putDouble("key1",remoteMessage.getData().get("title"));
bundle.putDouble("key2",remoteMessage.getData().get("coolNews1"));
......
intent.putExtras(bundle);
sendBroadcast(intent);
并启动广播类
和
DONT FORGET TO ADD TO YOUR MANIFEST!
类似这样的事情...
public class NotificationBR extends BroadCastReciever {
@Override
public void onReceive(Context ctx, Intent intent){
if(intent.getExtras() != null){
String key1 = intent.getStringExtra("key1");
......
}
//Then you can use intent to send it to whichever activity you want
Intent sendToActivity = new Intent(ctx, Activity.class);
sendToActivity.putExtras(someBundle) //Containing your strings you want to pass to activity, you can also use sendToActivity.putStringExtra("Some values")
startActivity(sendToActivity);
就是这样。
抱歉,有错别字。
关于java - 无法通过firebase-cloud-messaging中的pendingIntent传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55518224/
我想了解 PendingIntent.FLAG_NO_CREATE 的行为。我想知道它什么时候返回 null 什么时候不返回。这是事件发生的顺序: alarmIntent = new Intent(c
我正在尝试请求网络,因此我构建了 NetworkRequest 并构建了 PendingIntent, ConnectivityManager conn_manager = (Connectivit
我一直在使用警报管理器。一旦我设置了一个 PendingIntent 来触发警报,我想取消它。 How do I achieve this? 帮助将不胜感激。 最佳答案 下面这几行代码肯定可以帮助您删
我正在尝试在 Android 中创建一个 PendingIntent。这是代码 mNotificationIntent = new Intent(getApplicationContent(), My
我有三个 Activity (Home、Search、Destination),我可以用它们来描述我的用户体验流程。 Home Activity 是我的启动器 Activity ,然后是搜索 Acti
我试图从 API 中了解 PendingIntent 的用途。 有时我使用的一些方法需要这样做,但我仍然没有得到正确的想法。 谁能解释一下吗?为什么我不能只使用上下文? 谢谢 莫希克 最佳答案 Pen
我在 Android 中开发闹钟时遇到问题。当我触发包含必须在计划时间启动的 Activity 的 PendingIntent 时,它运行良好。但是,当闹钟响起并且我按贪睡 5 分钟并使用不同的请求代
我得到了一个 IntentService,它在 onHandleIntent(Intent) 中同步执行一些长时间运行的工作。因此,只要服务运行,我就会显示一个通知。现在我想在用户点击通知后停止服务。
我有这个... button = (Button) findViewById(R.id.start_repeating); button.setOnClickListener(new OnClickL
在我的应用程序中,我有一个显示 2 个选项的对话框:Copy 和 Dismiss 当用户单击 Copy 时,我希望在状态栏中收到通知。 public void showDialog(final Str
我有一个会显示通知的服务。通知显示正确,但当我点击通知时没有任何反应。它应该在我的应用程序中打开一个 Activity 。这是我的代码: public class ServiceCheckExpens
我正在我的游戏中创建一个通知。如果玩家有一段时间没有玩游戏,我想显示通知。我正在使用以下带有警报设置的 PendingIntent 来创建通知。有了这个,我就可以创建通知。 问题:如果玩家玩游戏,我需
所以这段代码是在我的服务开始在前台启动后从内部运行的。这也会创建将留在那里的通知。我有很多问题/疑问: 1) 点击通知只会打开应用信息,不会打开我的 PendingIntent。 2) 未设置我的内容
如何创建多个 PendingIntent?我有几个通知,当用户按下最后一个通知时,一切正常,但点击通知没有响应。我的代码是: Intent notificationIntent = new Inten
我正在运行一项服务,该服务会在特定事件的通知栏上发出通知。在该通知中,我设置了一个 PendingIntent 以启动另一个应用程序。出于某种原因,当我根据通知采取行动时,它不会启动其他应用程序。这是
我有一个名为 sendNotification 的方法,可以多次调用。 private PendingIntent createNotificationIntent(Context context,
我有一个导航应用程序。我想获得可能暂时不会到达的位置信息,具体取决于 GPS 锁定所需的时间——或者接收信号不好时的时间。 我计划使用 LocationManager.requestLocationU
我有一个 Android 库,它有一个创建 Notification 的 Service。据我了解,Notification 必须有一个 contentIntent (PendingIntent) 集
当我们将 0 作为标志传递给 PendingIntent 时,如下所示: PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0); does
我在尝试通过 Intent 和未决 Intent 将数据传递给 BroadcastReceiver 时遇到了一些问题,涉及接近警报。更具体地说,我试图传递一个对象,其中包括用户不断变化的位置。我已经尝
我是一名优秀的程序员,十分优秀!