这是我的清单
<service android:name=".fcm.PshycoFirebaseMessagingServices">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".fcm.PshycoFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
当应用程序处于后台并且通知到达时,默认通知将会运行并且不会运行我的代码onMessageReceived.
这是我的onMessageReceived代码.如果我的应用程序在前台运行,而不是在后台应用程序时,则调用此方法.如何在应用程序处于后台时运行此代码?
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See …Run Code Online (Sandbox Code Playgroud) 我正在使用Firebase,并在应用程序处于后台时测试从我的服务器向我的应用发送通知.通知成功发送,甚至出现在设备的通知中心,但是当通知出现或甚至我点击它时,我的FCMessagingService内的onMessageReceived方法永远不会被调用.
当我在我的应用程序位于前台时对此进行测试时,调用了onMessageReceived方法,一切正常.当应用程序在后台运行时会出现此问题.
这是预期的行为,还是有办法解决这个问题?
这是我的FBMessagingService:
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FBMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.i("PVL", "MESSAGE RECEIVED!!");
if (remoteMessage.getNotification().getBody() != null) {
Log.i("PVL", "RECEIVED MESSAGE: " + remoteMessage.getNotification().getBody());
} else {
Log.i("PVL", "RECEIVED MESSAGE: " + remoteMessage.getData().get("message"));
}
}
}
Run Code Online (Sandbox Code Playgroud) 我制作了一个应用程序,可以在Android手机的下拉状态栏中设置通知.但是,我的代码中存在一个错误(有时会设置通知,有时则不会).我希望能够检查(在代码中)如果通知对用户是可见的.(即用户可以在状态栏中看到通知吗?).
我怎样才能做到这一点?(提前致谢).
非常感谢示例代码.
应明确实施如何使用Firebase通知和数据.我读了很多答案,但似乎无法使其发挥作用.这是我的步骤:
1.)我在PHP中将通知和数据传递给android,它看起来很好:
$msg = array
(
"body" => $body,
"title" => $title,
"sound" => "mySound"
);
$data = array
(
"user_id" => $res_id,
"date" => $date,
"hal_id" => $hal_id,
"M_view" => $M_view
);
$fields = array
(
'registration_ids' => $registrationIds,
'notification' => $msg,
'data' => $data
);
$headers = array
(
'Authorization: key='.API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( …Run Code Online (Sandbox Code Playgroud) notifications android firebase firebase-realtime-database firebase-cloud-messaging
我希望在收到通知时自动打开应用程序,Firebase和新的FCM通知是否可以实现?
我知道我可以设置click_action,但这只是为了自定义通知点击会启动哪些活动,我需要在收到通知时自动启动的内容.
我尝试了快速启动消息传递firebase示例,并且有一个onMessageReceived()方法,但只有在应用程序位于前台时它才有效.应用程序在后台运行时是否会执行某些操作?GCM可以通过直接启动广播接收器的活动意图来做我喜欢的事情,广播接收器在收到通知时调用.
android firebase firebase-cloud-messaging firebase-notifications
我想在我的应用中制作一个简单的通知功能.我按照此 YouTube视频和这两个Firebase文档网址1和2以及Android Studio中的Firebase工具助手(说我已连接到Firebase).出于某种原因,在我的旧应用程序(下面发布的代码)上执行这些步骤和文档后,它将不允许我接收通知.但是,如果我在一个全新的应用程序上执行相同的步骤,它将完美地运行.我在后台,活动和终止状态下测试了同一物理设备和环境中的两个应用程序.每次我创建的新演示应用程序都没有问题,但我想要通知的旧应用程序不起作用.两者均未经过设备ID测试.我甚至没有得到任何错误日志或任何TAG日志.我认为我的一个编译项目是干扰的,不确定究竟是什么,但我可能要看那里.
PS.我在下面删除了我的包名,我在FireBase上多次检查并且它们匹配,所以我知道这不是问题.但是,我的新演示应用程序的FireBase显示我的应用程序连接但我的旧应用程序的FireBase没有.此外,我之前已经设置了赏金,但仍然遇到同样的问题.
这是我有问题的代码:
Notification.java(它的文档要求的服务)
public class Notification extends FirebaseMessagingService {
public Notification() {
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
showNotification(remoteMessage.getData().get("message"));
Log.d("FMC", "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
private void showNotification(String message) {
Intent i=new Intent(this, SplashScreen.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("FCM TITLE").setContentText(message)
.setSmallIcon(R.drawable.pt_icon)
.setDefaults(android.app.Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent);
NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,builder.build());
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我甚至没有在上面的notification.java中登录.
Project.gradle
buildscript {
repositories {
jcenter()
maven {
url 'https://maven.google.com/' …Run Code Online (Sandbox Code Playgroud) 按我以前问的问题,火力地堡onMessageReceived不叫当应用程序在后台,我需要的有效载荷更改为"数据"的有效载荷,而不是一个"通知"的有效载荷.(请参阅此处的链接 - Firebase推送通知和FCM消息之间有什么区别?).
问题是,无论是IOS和Android的应用程序,我们必须利用火力地堡和IOS应用程序需要推送通知有效载荷使用"通知"结构,而Android需要"数据"有效载荷结构.
因此,我的问题是,您如何区分通过firebase sdk获得的Android和IOS令牌/ ID?
如果我们的服务器保存这些ID并需要发送推送通知,则需要指定Android与IOS以更改有效负载结构.完成此识别的唯一方法是通过基于应用程序的服务调用来区分IOS和Android吗?或者是否有更复杂的方法使用Firebase,这将允许我们指出它是哪个设备?
谢谢大家.
我通过FCM实现了推送通知。当应用程序从服务器收到新通知时,“通知面板”条会被我在NotificationCompat.Builder中设置的图标所注意到,但是该消息不会作为弹出窗口预览。我尝试设置优先级,样式,类别,但仍未显示通知。滚动时可以看到通知。
我在2种不同的设备操作系统(6.0.1&5.0.1)上尝试了该应用程序,也是我的后端C#解决方案-两种方法都不会弹出通知-消息和通知
表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.theApp"
android:versionCode="31"
android:versionName="0.3.4" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.theApp.SplashScreen"
android:screenOrientation="portrait"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.theApp.MainView"
android:windowSoftInputMode="adjustResize"
android:screenOrientation="portrait"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.NoActionBar" >
</activity>
<service android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".FirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
FirebaseMessagingService
package com.theApp;
import android.app.Notification; …Run Code Online (Sandbox Code Playgroud) 我已经在Huawei AppGallery上发布了一个Android应用程序,并且能够通过HMS Push Service从我的应用程序后端服务器向手机发送推送通知(非透明),如下所述。
但是,我想知道如何在应用程序中访问推送通知有效内容:
这是我目前发送推送通知的方式-
首先,我到https://login.vmall.com/oauth2/的后端POST标记以下内容:
grant_type=client_credentials&client_id=101130655&client_secret=XXXXX
Run Code Online (Sandbox Code Playgroud)
并成功从HMS后端获取访问令牌:
{
"access_token":"CF1/tI97Ncjts68jeXaUmxjmu8BARYGCzd2UjckO5SphMcFN/EESRlfPqhi37EL7hI2YQgPibPpE7xeCI5ej/A==",
"expires_in":604800
}
Run Code Online (Sandbox Code Playgroud)
然后,我的后端POST到(对进行URL编码{"ver":"1", "appId":"101130655"})-
https://api.push.hicloud.com/pushsend.do?nsp_ctx=
%7B%22ver%22%3A%221%22%2C+%22appId%22%3A%22101130655%22%7D
Run Code Online (Sandbox Code Playgroud)
以下URL编码的正文:
access_token=CF1/tI97Ncjts68jeXaUmxjmu8BARYGCzd2UjckO5SphMcFN/EESRlfPqhi37EL7hI2YQgPibPpE7xeCI5ej/A==
&nsp_svc=openpush.message.api.send
&nsp_ts=1568056994
&device_token_list=%5B%220869268048295821300004507000DE01%22%5D
&payload=%7B%22hps%22%3A%7B%22msg%22%3A%7B%22action%22%3A%7B%22param%22%3A%7B%22appPkgName%22%3A%22de%2Eslova%2Ehuawei%22%7D%2C%22type%22%3A3%7D%2C%22type%22%3A3%2C%22body%22%3A%7B%22title%22%3A%22Alexander%3A+How+to+access+payload%3F%22%2C%22content%22%3A%22Alexander%3A+How+to+access+payload%3F%22%7D%7D%2C%22ext%22%3A%7B%22gid%22%3A86932%7D%7D%7D
Run Code Online (Sandbox Code Playgroud)
payload值在哪里(我不确定,它是否具有正确的JSON结构以及“类型” 3的真正含义):
{
"hps": {
"msg": {
"action": {
"param": {
"appPkgName": "de.slova.huawei"
},
"type": 3
},
"type": 3,
"body": {
"title": "Alexander:+How+to+access+payload?",
"content": "Alexander:+How+to+access+payload?"
}
},
"ext": {
"gid": 86932
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要提取自定义整数“ gid”值(我的应用中的“游戏ID”)。
在自定义接收器类中,我定义了以下方法,但未调用它们(该onToken方法除外-当我的应用启动并通过调用HuaweiPush.HuaweiPushApi.getToken方法异步从HMS请求“推送令牌”时):
public class MyReceiver …Run Code Online (Sandbox Code Playgroud) android push-notification huawei android-push-notification huawei-mobile-services