我很好奇 GCM 和 FCM 的情况如何 - 我看到 GCM 正在被弃用,但这在它被弃用之前仍然具有相关性。
android push-notification google-cloud-messaging android-push-notification
我已经在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
我正在使用FirebaseMessagingService我的推送通知。它按照应用程序中的预期工作。onMessageReceived被调用,我可以获取通知标题和正文+数据有效负载,并根据有效负载数据发送反应到我Activity使用的LocalBroadcastManager.
但后台推送通知存在问题。这只会在后台显示通知,但不会创建它,onMessageReceived因为如果应用程序在后台,则无法调用此函数。
根据文档,推送通知的数据部分存储extras在 Activity 恢复时可以找到。我已经有这个功能并且它正在工作。但问题是我没有标题和消息,因为它没有发送到onMessageReceived,我无法捕获此信息。
有什么方法可以获取吗?因为我需要在应用程序内的对话框窗口中显示它。推送通知不仅仅是文本和标题,也不仅仅是向用户提供的信息,而是会执行一些操作。
接收通知标题、正文和负载FirebaseMessagingService:
override fun onMessageReceived(msg: RemoteMessage?) {
val pNotification = msg?.notification
val payload = msg?.data
if (pNotification != null){
val ntitle = pNotification.title
val nMsg = pNotification.body
}
//working with payload - creating notification and Intent for LocalBroadcastmanager
}
Run Code Online (Sandbox Code Playgroud)
从内部的额外部分捕获有效负载onResume()
private fun maybeFindBackgroundPayload(extras: Bundle?){
if (extras != null){
extras.keySet().forEach { key->
val keyValue = extras[key]
App.log("BackgroundKeyValues: $keyValue")
if …Run Code Online (Sandbox Code Playgroud) android android-service firebase firebase-cloud-messaging android-push-notification
我正在开发一个 Android TV 应用程序,其中我有一个用例,可以在收到推送通知后触发一些视图。在开始之前,我想知道 Android TV 是否像手机和平板电脑一样支持推送通知?
我不想向用户显示通知,我会将其视为无声通知,并且我想解析有效负载内容并在通知到达后在电视上显示新视图。用于手机/平板电脑的正常通知处理在这里可以工作吗?
android android-tv firebase-cloud-messaging android-push-notification
我一直在到处搜索,但就是不知道如何获取正在运行的 Android 应用程序的当前(顶部)活动。
\n\n好吧,我的情况是,当应用程序位于前台并且调用\xe2\x80\x8bonMessageReceived(FirebaseMessagingService 的子类)时,我收到了 Firebase Cloud Messaging Data 负载。我需要找出用户正在查看的屏幕,然后决定关闭(finish())它或发送一些数据(通过 Extras / Bundle)并刷新视图。
\n\n那么,我如何找出当前的视图/活动是什么并讨论或发送一些数据并导致视图刷新?
\n\n谢谢!
\nandroid push-notification firebase firebase-cloud-messaging android-push-notification
我正在实施位于https://github.com/firebase/quickstart-android/tree/master/messaging的 Firebase Cloud Messaging Quickstart 示例项目,并将其合并到我的应用程序中。在https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MainActivity.java我可以看到以下代码块:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create channel to show notifications.
String channelId = getString(R.string.default_notification_channel_id);
String channelName = getString(R.string.default_notification_channel_name);
NotificationManager notificationManager =
getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW));
}
Run Code Online (Sandbox Code Playgroud)
使用条件的目的是什么if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){}?据我了解,Build.VERSION.SDK_INT返回在安装该应用的设备的API级别,并且Build.VERSION_CODES.O是我定义的API级别的应用程序/文件的build.gradle针对编译,例如:compileSdkVersion 26。如果用户的设备的 API 级别低于compileSdkVersion我用来定义我正在编译的 SDK 版本的 API 级别,代码是否要求不执行创建通道以显示通知的代码?我不明白该条件的目的。顺便说一句,我正在使用 API 级别为 23 且符合预期的电话进行测试,因为我compileSdkVersion 26在我的build.gradle文件,整个代码块都没有被执行。如果您能帮助阐明这段代码的目的,我将不胜感激,当然这不是我写的代码。我从https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MainActivity.java 获取它,但我正在尝试去理解它。谢谢你。
android push-notification firebase-cloud-messaging android-push-notification
我在https://developers.google.com/instance-id/reference/server#get_information_about_app_instances找到以下示例 GET 请求:
https://iid.googleapis.com/iid/info/nKctODamlM4:CKrh_PC8kIb7O...clJONHoA?details=trueAuthorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
Run Code Online (Sandbox Code Playgroud)
该示例中的关键是:AIzaSyZ-1u...0GBYzPu7Udno5aA。我在哪里可以找到我应该在 GET 请求中使用的密钥?我试图在https://console.developers.google.com/apis/credentials找到它,这就是我看到的:
我尝试使用上图中看到的所有四个键,但我总是从浏览器收到此消息:
{"error":"MissingAuthorization"}
Run Code Online (Sandbox Code Playgroud)
这个错误是指密钥错误还是其他原因?我是否在正确的地方寻找钥匙?谢谢。
编辑1:我正在查看如何检查已订阅了多少主题?,我还尝试使用在 Firebase 控制台的“Cloud Messaing”选项卡下找到的密钥。这是我所看到的:
我也尝试使用上图中的这些键进行 GET 请求,但仍然看到相同的错误:{"error":"MissingAuthorization"}。
编辑2:我从命令行使用它:
C:\curl>curl -k https://iid.googleapis.com/iid/info/nKctODamlM4:CKrh_PC8kIb7O...clJONHoA?details=trueAuthorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{"error":"MissingAuthorization"}
C:\curl>
Run Code Online (Sandbox Code Playgroud)
请注意,我如何简单地复制/粘贴在“示例 GET 请求”标题下的https://developers.google.com/instance-id/reference/server#get_information_about_app_instances中找到的内容。我什至没有尝试使用自己的密钥,但我看到了相同的错误:{"error":"MissingAuthorization"}。这是否意味着问题出在我使用的密钥上?这不是我在 Firebase 控制台中看到的吗?
android push-notification firebase firebase-cloud-messaging android-push-notification
有几个通知,点击通知我想转到特定活动。假设出现奖励通知,点击将转到 RewardActivity 并添加新朋友,然后单击 newFriend 通知将转到friendlistActivity,无论应用程序是在前台还是后台。
但在我的情况下,如果出现任何通知,它会转到相同的活动而不是不同的活动。
private void handleDataMessage(String noti_title,String noti_message,String noti_click_action) {
try {
Intent intent = new Intent(this, RewardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("title", noti_title);
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
//image.compress(Bitmap.CompressFormat.PNG, 50, _bs);
//intent.putExtra("img", image);
intent.putExtra("msg", noti_message);
intent.putExtra("click_action", noti_click_action);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this, "Default")
//.setLargeIcon(R.mipmap.ic_launcher)/*Notification icon image*/
.setSmallIcon(R.mipmap.ic_launcher)
//.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_HIGH)
.setChannelId("Default")
.setVibrate(new long[]{1000, 1000})
.setContentIntent(pendingIntent);
notificationBuilder.setContentTitle(noti_title);
notificationBuilder.setContentText(noti_message);
notificationBuilder.setAutoCancel(true);
NotificationManager notificationManager …Run Code Online (Sandbox Code Playgroud) 我最近在我的 React Native 应用程序中设置了推送通知,它在 iO 中运行良好,但在 Android 中无法弹出。这是我调用来触发通知的函数。
function pushNotif(){
PushNotification.localNotification({
channelId: "specialid", // (required) channelId, if the channel doesn't exist, notification will not trigger.
title: "hello",
message: "test message"
})
}
Run Code Online (Sandbox Code Playgroud)
这是我配置上面推送通知的地方
// Must be outside of any component LifeCycle (such as `componentDidMount`).
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function (token) {
console.log("TOKEN:", token);
},
// (required) Called when a remote is received or opened, or local notification is opened
onNotification: function (notification) …Run Code Online (Sandbox Code Playgroud) 当我使用 Firebase 控制台时,通过激活“高级选项”下的“声音”,一切都很完美,正如最佳答案 Mouad Abdelghafour AitAli 在Firebase Push notification is not Playing sound while app is in back 的接受答案中所解释的那样。当我使用 cURL 向端点发出请求时https://fcm.googleapis.com/fcm/send,我收到通知,唯一的问题是通知不播放声音。这就是我从命令提示符发送通知的方式:
C:\curl>curl -k -X POST --header "Authorization: key=<My authorization key>" --header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"<Token of device that receives the notification>\",\"notification\":{\"body\":\"Yellow\"},\"priority\":10,\"sound\":\"default\"}"
{"multicast_id":4910170660116070247,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1512975815064180%efad4c69efad4c69"}]}
Run Code Online (Sandbox Code Playgroud)
请注意我如何使用\"sound\":\"default\",但通知仍然没有播放声音。正如我提到的,它可以在 Firebase 控制台上运行,所以我试图理解为什么它不能从命令行使用 cURL 运行。谢谢。
android curl firebase firebase-cloud-messaging android-push-notification
我正在Android应用程序中实现推送通知.我们正在使用OneSignal发送推送通知.我们有一个API可以跟踪所有设备和API,决定哪些设备发送推送通知.
API请求OneSignal和OneSignal向设备发送通知.
在Android中,我使用以下代码获取设备令牌,该代码保存在API上,并使用它来发送推送通知.
public String getGCMTokenForPushNotification(){
String token = "";
try {
InstanceID instanceID = InstanceID.getInstance(this);
token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.i(TAG, "GCM Registration Token: " + token);
return token;
}catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
}
return token;
}
Run Code Online (Sandbox Code Playgroud)
当Android应用程序在那里注册时,R.string.gcm_defaultSenderId是firebase上的SENDER ID.
截至目前,我已经在Android应用程序上设置了OneSignal
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
}
Run Code Online (Sandbox Code Playgroud)
https://documentation.onesignal.com/docs/android-sdk-setup
如果API正在决定并在信号上初始化设备并发送推送通知.我不认为我会在Android应用程序上要求OneSignal设置.是对的吗?如果我从Android应用程序中删除OneSignal.我应该用什么来处理通知.
背后的原因是,截至目前我们正在使用OneSignal,将来如果我们更改提供商,我们不必在应用程序方面做任何事情.
是否有任何geniric方式来处理Android上的通知请.
谢谢您的帮助.[R
android push-notification google-cloud-messaging onesignal android-push-notification
当应用程序在后台或被杀死 onRecive onMessageReceived() 未调用时,我在通知中只收到 json。下面是我的服务类
public class AppFireBaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
else if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"));
}
}
@Override
public void …Run Code Online (Sandbox Code Playgroud) android ×12
firebase ×4
fcmp ×2
android-tv ×1
curl ×1
huawei ×1
ios ×1
onesignal ×1
react-native ×1