标签: firebase-cloud-messaging

firebase 设置后台消息处理程序

我一直在尝试在前端自定义通知消息,即如果未设置字段发送通知,我正在尝试添加它。

importScripts('https://www.gstatic.com/firebasejs/5.0.4/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.0.4/firebase-messaging.js');

var config = {
    apiKey: "x",
    authDomain: "y",
    databaseURL: "z",
    projectId: "a",
    storageBucket: "b",
    messagingSenderId: "1"
};

firebase.initializeApp(config);

const messaging = firebase.messaging();
console.log('came here');

console.log(messaging.bgMessageHandler);

console.log(messaging.setBackgroundMessageHandler,'dsafdsadasfd')




messaging.setBackgroundMessageHandler(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // Customize notification here
  var notificationTitle = 'Background Message Title';
  var notificationOptions = {
    body: 'Background Message body.',
    icon: '/firebase-logo.png'
  };

  console.log(notificationOptions)
  return self.registration.showNotification(notificationTitle,
    notificationOptions);
});

console.log(messaging.bgMessageHandler);
Run Code Online (Sandbox Code Playgroud)

在执行上面的代码时[firebase-messaging-sw.js] Received background message ', payload,即使我收到通知,我也没有收到 的控制台。

为什么setBackgroundMessageHandler不工作?

firebase google-cloud-messaging service-worker firebase-cloud-messaging

2
推荐指数
1
解决办法
4676
查看次数

FCM api '错误请求 400' 错误

我正在尝试通过新的 FCM api 协议实现发送通知。我经历了身份验证请求,我也在谷歌控制台中启用了 api。现在,当我发送请求时,我收到错误请求 400 错误。除了错误代码和错误消息外,响应不包含任何其他信息。甚至原因字段显示“错误请求”。我正在使用FCM api docs实现它。

def fcm_test():
    FCM_URL = "https://fcm.googleapis.com/v1/projects/MY_PROJECT_NAME/messages:send"
    my_token = "device-token"

    payload = {
        "massage": {
            "token": my_token,
            "notification": {
                "body": "body",
                "title": "title"
            }
        }
    }

    payload = json.dumps(payload)
    headers = {
        "Authorization": "Bearer {auth_token}".format(auth_token=_get_authentication_token()),
        "Content-Type": "application/json; UTF-8",
    }
    try:
        request = moves.urllib.request.Request(FCM_URL, payload.encode('utf-8'), headers)
        response = moves.urllib.request.urlopen(request, timeout=10)
    except Exception as e:
        pass
    return
Run Code Online (Sandbox Code Playgroud)

python api python-3.x firebase firebase-cloud-messaging

2
推荐指数
1
解决办法
2675
查看次数

为 iOS 的 FCM 推送通知启用默认声音

我正在尝试为发送的推送通知设置默认声音,并让以下消息对象通过。

var message = {
      notification: {
        title: "X",
        body: "X",
        sound: 'default'
      }          
      token: fcmToken
 };
Run Code Online (Sandbox Code Playgroud)

但是,我继续在云函数日志中收到以下错误:

发送消息时出错:错误:收到无效的 JSON 负载。“message.notification”中的未知名称“声音”:找不到字段。

我尝试将声音属性放在通知对象 apns-payload-aps 对象下,但似乎无法找到有关激活 iOS 上默认声音的正确语法的文档。

任何帮助表示赞赏。

push-notification ios firebase google-cloud-functions firebase-cloud-messaging

2
推荐指数
1
解决办法
4776
查看次数

如果应用程序被卸载,Firebase 从数据库中删除相关行

我正在使用 Cloud Firestore 来保存应用程序令牌以发送推送通知。但是,当用户卸载并重新安装应用程序时,Firestore 会收到同一用户的不同令牌。当用户卸载应用程序时,如何删除上一个令牌的相关行?

提前致谢。

android firebase firebase-cloud-messaging google-cloud-firestore

2
推荐指数
1
解决办法
1166
查看次数

如何在 Swift 中的 didReceiveRemoteNotification 上从 App Delegate 推送视图控制器?

我找不到任何明确的答案,我开始挣扎。到目前为止,我最接近的是present所需的视图控制器,但显然它以模态显示,而不是使用选项卡栏或导航栏。

我在我AppDelegatedidReceiveRemoteNotification函数中尝试处理 Firebase 推送通知。基本上,我希望能够根据我在 Firebase 控制台上分配的键值对自动切换到我的应用程序中的特定视图控制器。

到目前为止,我的 App Delegate 看起来像这样:

应用程序委托

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        let info = userInfo as NSDictionary
        let name: String = info.value(forKey: "view-controller") as! String

    switch name {
    case "controller1":
        print("controller1")
        let storyBoard = UIStoryboard.getMainStoryboard().instantiateInitialViewController()
        UIApplication.shared.delegate!.window!!.rootViewController = storyBoard
        DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
            print("Time now reached")
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller1VC = storyboard.instantiateViewController(withIdentifier: "controller1VC") …
Run Code Online (Sandbox Code Playgroud)

uiviewcontroller apple-push-notifications ios swift firebase-cloud-messaging

2
推荐指数
1
解决办法
1888
查看次数

在应用程序处于后台/终止时处理推送通知 (FCM) - Android

我想要什么:我想从Firebase Console发送通知。当应用程序处于后台/终止状态并且用户点击推送通知时,它可以转到特定活动。我们称之为Current Offers Activity。当应用程序处于前台时,应该会发生同样的事情。

我的理解:firebase docs,当应用程序处于前台时,将从Firebase 控制台发送的推送通知将在类的onMessageReceived方法中接收MyFirebaseMessagingService。但是当应用程序在后台/终止时,不会调用类的onMessageReceived方法MyFirebaseMessagingService

我想要什么:当应用程序处于后台/终止和用户点击推送通知时,我想将用户重定向到特定活动(当前优惠活动)。

我试过这个答案,但我没有得到解决方案。

我的代码如下。

MyFirebaseMessagingService.java

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private final String TAG = "mk";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Log.e(TAG, "Message Body: " + remoteMessage.getNotification().getBody());
    }
}
Run Code Online (Sandbox Code Playgroud)

MyFirebaseInstanceIDService.java

    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private final String TAG = "mk";

    @Override
    public void onTokenRefresh() …
Run Code Online (Sandbox Code Playgroud)

deep-linking android-intent android-activity firebase firebase-cloud-messaging

2
推荐指数
2
解决办法
6763
查看次数

Google API PHP 客户端服务中是否有 FCM 服务

我已经浏览了 Github,但找不到任何相关的类或文档。

https://github.com/google/google-api-php-client-services/tree/master/src/Google/Service

我正在尝试从服务器向 Web 客户端发送 FCM 消息。以下是我目前如何实现这一目标。

<?php
header('Content-Type: text/json');

$data = array(
    'message' => array(
        'notification' => array(
            'title' => 'FCM Message',
            'body' => 'This is an FCM Message',
        )
    )
);

$server_key = 'ya29.ElqKBGN2Ri_Uz...HnS_uNreA';

$url = 'https://fcm.googleapis.com/fcm/send';
$headers = 'Authorization:key = '.$firebase_api_key."\r\n".'Content-Type: application/json'."\r\n".'Accept: application/json'."\r\n";

$registration_ids = array('bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...');
$fields = array('registration_ids' => $registration_ids, 'data' => $data);

$content = json_encode($fields);
$context = array('http' => array( 'method' => 'POST', 'header' => $headers, 'content' => $content));
$context = stream_context_create($context);
$response = …
Run Code Online (Sandbox Code Playgroud)

php google-api-php-client firebase-cloud-messaging

2
推荐指数
1
解决办法
2421
查看次数

如何知道 Service Worker 中是否激活了推送通知的权限?

我正在开发一个渐进式网络应用程序(PWA)项目,该项目在服务工作者中制作推送通知。使用 FCM(Firebase Cloud Messaging)发送推送通知是成功的,但我想知道我如何知道我的网络应用程序是否有权从用户的浏览器发送推送通知。如果可能,我想显示用户按钮以请求推送通知的权限。是否可以?

firebase service-worker progressive-web-apps firebase-cloud-messaging

2
推荐指数
1
解决办法
2006
查看次数

错误:(15, 55) 错误:无法访问 com.google.android.gms.common.internal.safeparcel.zza 的 zza 类文件未找到

这是我的 build.gradle 应用程序文件:

dependencies {
    //picture library
    compile 'com.github.bumptech.glide:glide:3.8.0'
    //recycler and card view
    compile 'com.android.support:support-v4:26.+'
    compile 'com.android.support:recyclerview-v7:26.+'
    compile 'com.android.support:cardview-v7:26.+'

    //json
    compile 'com.google.code.gson:gson:2.8.5'
    compile 'com.squareup.retrofit2:converter-gson:2.2.0'
    compile 'com.squareup.retrofit2:retrofit:2.4.0'
    //firebase
    compile 'com.google.firebase:firebase-core:16.0.1'
    compile 'com.google.firebase:firebase-messaging:10.0.1'
    testCompile 'junit:junit:4.12'
}

apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)

这是我的 build.gradle 项目文件

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        classpath 'com.google.gms:google-services:4.0.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()

        maven { url …
Run Code Online (Sandbox Code Playgroud)

android firebase build.gradle firebase-cloud-messaging

2
推荐指数
1
解决办法
6547
查看次数

应用程序打开时,Firebase 云消息传递不显示通知栏

我试图通过在 firebase 云消息传递中通过控制台发送通知来测试我的应用程序,但是当我的应用程序在前台或仍在运行时。通知不会显示在设备中,并且触发了 firebase 消息传递服务中的日志。

但是当我销毁我的应用程序时,通知工作得很好。

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.yehezkiel.eclassapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/eclass_logo1"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/eclass_logo1"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:icon">


        <service
            android:name=".myFirebaseInstanceTokenID">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity" />
        <activity android:name=".Tugas" />
        <activity
            android:name=".DetailActivity"
            android:label="@string/title_activity_detail"
            android:theme="@style/AppTheme" />
        <activity android:name=".NilaiActivity" />
        <activity android:name=".BaseActivity" />
        <activity android:name=".MateriActivity" />
        <activity android:name=".NilaiDetailActivity" />
        <activity android:name=".PesertaActivity"></activity>


    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

这是我的 firebasemessagingservice

public …
Run Code Online (Sandbox Code Playgroud)

android push-notification firebase firebase-cloud-messaging

2
推荐指数
1
解决办法
5919
查看次数