我写了一个非常简单的Android应用程序来测试firebase推送通知,我收到两次通知.
这是清单服务:
<service
android:name="com.google.firebase.messaging.FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name="com.google.firebase.iid.FirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
这是应用程序gradle:
compile 'com.google.android.gms:play-services:9.0.0'
compile 'com.google.firebase:firebase-core:9.0.0'
compile 'com.google.firebase:firebase-messaging:9.0.0'
}
apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)
这是项目级别的gradle:
classpath 'com.google.gms:google-services:3.0.0'
Run Code Online (Sandbox Code Playgroud) android firebase firebase-cloud-messaging firebase-notifications
我正在开发一个电子邮件应用程序,我希望用户在收到新电子邮件后立即收到推送通知。为此,我正在使用 FCM。我刚刚通过以下链接尝试使用 fcm 推送通知:https : //www.youtube.com/watch?v= XijS62iP1Xo&t=6s 以测试 fcm 提供的功能。但我面临的问题是,当应用程序处于前台或后台时,设备会收到推送通知,但在应用程序关闭时(从任务管理器滑动或清除),它不会收到任何推送通知。我不知道如何通过 fcm 实现这一目标?我想像 WhatsApp 和 facebook 一样接收推送通知。
各种帮助表示赞赏。提前致谢。
我正在使用FCM进行简单通知
当应用程序处于前台时,一切正常.我收到通知以及onMessageReceived方法内的数据消息.
但是当应用程序处于后台时,我会在系统托盘中收到通知.当我点击控件时,它会转到主要活动.当我分析intent.getExtras();,我只得到这个关键的数据- ,google.sent_time,from,.google.message_idcollapse_key
如何获取系统托盘中可见的通知消息标题和消息intent.getExtras()?
我正在使用FCM控制台发送通知我没有我的专用服务器来执行此操作.
接收邮件的代码:
final Bundle extras = intent.getExtras();
final Set<String> keySet = extras.keySet();
final Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
final String key = iterator.next();
final Object o = extras.get(key);
System.out.println(key + ":" + o);
}
Run Code Online (Sandbox Code Playgroud) android firebase firebase-cloud-messaging firebase-notifications
我已为我的 android 应用启用 Firebase 应用内消息传递。当我测试应用内消息时,它显示在应用的 SplashActivity 中。
注意:SplashActivity 刚刚可以运行以获得 3 秒的延迟。LoginActivity 有一些函数来检查共享首选项是否为空。
我试图在 onCreate() 下面的代码行中添加:
FirebaseInAppMessaging.getInstance().setMessagesSuppressed(true)
而FirebaseInAppMessaging.getInstance().setMessagesSuppressed(false)在的onDestroy()
我希望此消息显示在 MainActivity 中。
当我的应用程序打开并收到通知时,我希望能够立即打开相关活动,而无需用户点击通知.
这个问题非常相似:收到firebase通知的打开应用程序(FCM)
但它在应用程序处于后台时打开应用程序,我需要在应用程序处于前台时执行此操作.
当您的应用在后台时发送通知.在这种情况下,通知将传递到设备的系统托盘.用户点按通知会默认打开应用启动器.具有通知和数据有效负载的消息,包括后台和前台.在这种情况下,通知将传递到设备的系统托盘,并且数据有效负载将在启动器活动的附加内容中传递.
这是我的命令 onMessageReceived
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification( remoteMessage);
}
}
/**
* Create and show a simple notification containing the received FCM message.
*
* @param remoteMessage FCM message …Run Code Online (Sandbox Code Playgroud) android firebase firebase-cloud-messaging firebase-notifications
我在Android应用中使用Firebase作为我们的消息服务.我已经对Firebase进行了相当多的研究,我理解无论应用程序是在前台运行还是现在运行都会改变onMessageReceived方法中收到的数据类型.
我想要完成的是解析来自Remotemessage的传入数据,并根据自定义标记执行与它不同的操作.IE,如果数据映射包含一个名为"My_Custom_Tag"的字段,我想完全覆盖弹出的标准firebase消息并编写一个自定义消息.
问题是,当应用程序在后台运行时,我放入onMessageReceived的任何代码都不会被触发.当应用程序在前台运行时,它可以正常工作,但如果应用程序在后台,则无法检测/接收任何内容.
以下是一些示例代码:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map<String, String> aMap = remoteMessage.getData();
//Custom object here for easier parsing
PushNotificationsPojo pojo = this.convertResponseToPojo(aMap);
if(pojo == null){
passToFirebase(remoteMessage);
return;
}
if(pojo.getData().getCustomTag().equals(PushNotificationsPojo.ID_TAG_CHAT_MESSAGE)) {
createCustomNotification1(pojo);
} else if (pojo.getData().getCustomTag().equals(PushNotificationsPojo.ID_TAG_SOME_OTHER_MESSAGE)){
createCustomNotification2(pojo);
} else if (pojo.getData().getCustomTag().equals(PushNotificationsPojo.ID_TAG_STUFF_AND_WHATNOT)) {
createCustomNotification3(pojo);
} else {
passToFirebase(remoteMessage);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何编辑MyFirebaseMessagingService以便我可以检查传入的数据,确定标记信息,并决定将其传递给firebase,以便它可以使用标准处理或不将其传递给firebase并编写我的我的应用程序在后台运行时,我自己的自定义显示通知?
谢谢大家!
我正在通过我自己的Web服务发送Firebase通知,如下所示.
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = SERVER_KEY ',
'Content-Type: application/json'
);
Run Code Online (Sandbox Code Playgroud)
1.当App在前台和后台没有任何问题时,通知即将到来,但如果我从最近的托盘中删除应用程序然后通知没有到来,我必须处理这个任何解决方案吗?(就像Whatsup通知显示所有场景甚至我强制停止此应用程序的设置)
2.一旦我收到通知,从"onMessageReceived"如何将这些消息,正文内容传递给我的Activity/Fragments?
3.在某些情况下,我想向所有人发送通知,而不是将所有令牌添加到数组中.任何其他方式处理像"发送给所有"这样的东西?
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Data Payload : " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("message"));
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri …Run Code Online (Sandbox Code Playgroud) notifications android android-notifications firebase firebase-cloud-messaging
我正在我的应用程序中实现FCM(Firebase消息服务).这里似乎都没问题,除非app处于后台状态我无法提取预期的通知数据.
基于概念:FCM中有两种类型的消息:
display-messages:这些消息仅在您的应用处于前台时才有效.
数据消息:即使您的应用处于后台,这些消息也能正常工作当我们的应用处于后台时,Android会将通知消息定向到系统托盘.
处理数据消息时,您的通知应具有click_action ="YOUR_ACTION"字段.
我的信息会是这样的:
{
"data": {
"body": "here is body",
"title": "Title",
"click_action": "YOUR_ACTION"
},
"to": "ffEseX6vwcM:APA91bF8m7wOF MY FCM ID 07j1aPUb"
}
Run Code Online (Sandbox Code Playgroud)
活动将显示清单文件将如下所示的消息:
<activity
android:name=".NotificationActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Dialog"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="YOUR_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
点击通知后,它将重定向到我的NotificationActivity.在我的NotificationActivityin onCreate和onNewIntent方法中,我使用这种方式提取消息:
Bundle bundle=getIntent().getExtras();
if(bundle!=null) {
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
Log.d("DATA_SENT", String.format("%s %s (%s)", key,
value.toString(), value.getClass().getName()));
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个Android应用程序,我正在使用一些方法在应用程序图标上显示通知编号.现在我想在收到通知时设置该号码.
我认为我应该在收到通知时设置数字,所以我在onMessageReceived方法中设置它.但是,我的问题是当我的应用程序处于后台时,onMessageReceived方法未被调用,因此未设置通知编号.
以下是我的代码.我在里面设置了号码onMessageReceived.我已经测试过setBadge方法并且可以验证它是否正常工作.问题是onMessageReceived没有调用所以setBadge也没有调用,没有设置数字.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Conts.notificationCounter ++;
//I am setting in here.
setBadge(getApplicationContext(),Conts.notificationCounter );
Log.e("notificationNUmber",":"+ Conts.notificationCounter);
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message …Run Code Online (Sandbox Code Playgroud) android google-cloud-messaging firebase-cloud-messaging firebase-notifications