当应用程序处于前景状态时,无法接收来自FCM的通知

时间:2018-11-10 07:57:42

标签: java android node.js firebase firebase-cloud-messaging

我是Android和编程的新手,我正在创建一个可接收来自FCM消息的应用程序,而该应用程序在后台运行,因此,我添加了一个FirebaseMessagingService类,该类在onMessageReceived方法中接收通知,然后跟随google文档,但在应用程序处于前台状态时,通知仍然不起作用,我已按照每个步骤进行操作,由于日志对我没有帮助,并且我未收到任何错误预期,因此我无法弄清代码中的问题所在这个我不知道是否是问题的来源:  在GCM事件java.lang.NumberFormatException中解析时间戳时出错:s == null

这是我的MyFirebaseMessagingService类

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;

import com.app.muhammadgamal.swapy.R;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

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

        String click_Action = remoteMessage.getNotification().getClickAction();


        String messageTitle = remoteMessage.getNotification().getTitle();
        String messageBody = remoteMessage.getNotification().getBody();


        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
                .setSmallIcon(R.drawable.ic_main_logo)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);


        Intent intent = new Intent(click_Action);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);


        int mNotificationID = (int)System.currentTimeMillis();
        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        manager.notify(mNotificationID,mBuilder.build());

    }
}

和我的清单文件

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.muhammadgamal.swapy">

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_main_logo"
        android:label="@string/app_name"
        android:roundIcon="@drawable/ic_main_logo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Activities.SignInActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activities.SignUpActivity"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Activities.ProfileActivity"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Activities.NavDrawerActivity"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Activities.SwapCreationActivity"
            android:theme="@style/AppTheme.NoActionBar" />

        <activity android:name=".Activities.ReceivedSwapRequest">
            <intent-filter>
                <action android:name="com.app.muhammadgamal.swapy.Activities.ReceivedSwapRequest_NOTIFICATION_TASK"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
        </activity>

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

    </application>

</manifest>

和我的索引函数

   'use strict'


const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


exports.sendNotification = functions.database.ref('/Notifications/{receiver_user_id}/{notification_id}')
.onWrite((data, context) =>
{
    const receiver_user_id = context.params.receiver_user_id;
    const notification_id = context.params.notification_id;


    console.log('We have a notification to send to :' , receiver_user_id);


    if (!data.after.val()) 
    {
        console.log('A notification has been deleted :' , notification_id);
        return null;
    }

    const DeviceToken = admin.database().ref(`/Users/${receiver_user_id}/device_token`).once('value');

    return DeviceToken.then(result => 
    {
        const token_id = result.val();

        const payload = 
        {
            notification:
            {
                title: "New Swap Request",
                body: `you have a new Swap Request, Please Check.`,
                click_action: "com.app.muhammadgamal.swapy.Activities.ReceivedSwapRequest_NOTIFICATION_TASK"
            }
        };

        return admin.messaging().sendToDevice(token_id, payload)
        .then(response => 
            {
                return console.log('This was a notification feature.');
            });
    });
});

1 个答案:

答案 0 :(得分:1)

我发现了问题,这是因为我使用的是Android O,并且未设置通知的通道ID,如果要在Android 8.0或更高版本上接收该通知,这是必须的

这是解决方案代码

theory termination_problem
  imports 
    Complex_Main
    "HOL-Library.Finite_Map"
begin

datatype (plugins del: "size") t = A | B | C "(nat, t) fmap"

abbreviation "tcf ≡ (λ v::(nat × nat). (λ r::nat. snd v + r))"

interpretation tcf: comp_fun_commute tcf
proof 
  fix x y
  show "tcf y ∘ tcf x = tcf x ∘ tcf y"
  proof -
    fix z
    have "(tcf y ∘ tcf x) z = snd y + snd x + z" by auto
    also have "(tcf x ∘ tcf y) z = snd y + snd x + z" by auto
    ultimately have "(tcf y ∘ tcf x) z = (tcf x ∘ tcf y) z" by auto
    then show "(tcf y ∘ tcf x) = (tcf x ∘ tcf y)" by auto
  qed
qed

instantiation t :: size 
begin

primrec t_size :: "t ⇒ nat" where
AR: "t_size A = 0" |
BR: "t_size B = 0" |
CR: "t_size (C x) = 
  (Suc 0) + ffold tcf 0 (fset_of_fmap (fmmap t_size x))" 

definition size_t where
size_t_def: "size_t = t_size"

instance ..

end

lemma ffold_rec_exp:
  assumes "k |∈| fmdom x"
    and "ky = (k, the (fmlookup (fmmap t_size x) k))"
  shows "ffold tcf 0 (fset_of_fmap (fmmap t_size x)) = 
        tcf ky (ffold tcf 0 ((fset_of_fmap (fmmap t_size x)) |-| {|ky|}))"
  using assms tcf.ffold_rec by auto

lemma elem_le_ffold:
  assumes "k |∈| fmdom x"
  shows "t_size (the (fmlookup x k)) < 
        (Suc 0) + ffold tcf 0 (fset_of_fmap (fmmap t_size x))"
  using ffold_rec_exp assms by auto

lemma measure_cond [intro]:
  assumes "k |∈| fmdom x"
  shows "size (the (fmlookup x k)) < size (C x)"
  using assms elem_le_ffold size_t_def by auto

abbreviation
  "supc f xs ys ≡
    fmmap_keys
      (λk x. if (k |∈| fmdom ys) then (f x (the (fmlookup ys k))) else A)
      (fmfilter (λk. k |∈| fmdom ys) xs)"

fun sup_t (infixl "⊔" 65) where
  "A ⊔ _ = A"
| "B ⊔ x = (if x = B then B else A)"
| "C xs ⊔ x = (case x of C ys ⇒ C (supc sup_t xs ys) | _ ⇒ A)"

(*Examples*)

abbreviation "list_1 ≡ fmap_of_list [(1::nat, B)]"
abbreviation "list_2 ≡ fmap_of_list [(1::nat, A), (2::nat, A)]"
value "(C list_1) ⊔ (C list_2)"

abbreviation "list_3 ≡ fmap_of_list [(1::nat, B), (3::nat, A)]"
abbreviation "list_4 ≡ fmap_of_list [(2::nat, A), (4::nat, B)]"
value "(C list_3) ⊔ (C list_4)"

abbreviation "list_5 ≡ fmap_of_list [(1::nat, B), (2::nat, B)]"
abbreviation "list_6 ≡ fmap_of_list [(2::nat, B), (4::nat, B)]"
value "(C list_5) ⊔ (C list_6)"

abbreviation "list_7 ≡ 
  fmap_of_list [(1::nat, B), (2::nat, C list_5), (3::nat, A)]"
abbreviation "list_8 ≡ fmap_of_list [(2::nat, B), (4::nat, B)]"
value "(C list_7) ⊔ (C list_8)"

abbreviation "list_9 ≡ 
  fmap_of_list [(1::nat, B), (2::nat, C list_5), (3::nat, A)]"
abbreviation "list_10 ≡ fmap_of_list [(2::nat, C list_6), (3::nat, B)]"
value "(C list_9) ⊔ (C list_10)"

end