GVKun编程网logo

android – 当app在后台时,不会调用Firebase onMessageReceived

24

本文的目的是介绍android–当app在后台时,不会调用FirebaseonMessageReceived的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于android

本文的目的是介绍android – 当app在后台时,不会调用Firebase onMessageReceived的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于android Firebase Cloud Messages通知图标、Android In App Billing:即使成功购买,Receiver的onReceive也永远不会被调用、Android Mobile / Wear App onMessageReceived为一条消息调用多次、android Notification不会触发BroadcastReceiver的onReceive的知识。

本文目录一览:

android – 当app在后台时,不会调用Firebase onMessageReceived

android – 当app在后台时,不会调用Firebase onMessageReceived

我在Android应用中使用Firebase作为我们的消息服务.我对Firebase进行了相当多的研究,我理解whether the app if running in the foreground or now will change the type of data received in the onMessageReceived method.

我想要完成的是解析来自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);
    }
}

我的问题是,如何编辑MyFirebaseMessagingService以便我可以检查传入的数据,确定标记信息,并决定将其传递给firebase,以便它可以使用标准处理或不将其传递给firebase并编写我的我的应用程序在后台运行时,我自己的自定义显示通知?

谢谢大家!

PS,Firebase-Dungeon-Master Frank van Puffelen有什么机会有想法吗?

解决方法:

从FCM消息中删除通知有效内容,以便将数据有效负载传递给onMessageReceived方法.

仔细阅读this和this.

当您的应用程序位于后台时,仅当没有通知有效负载时,才会将数据有效负载传递到onMessageReceived方法. (标出单词)

如果两个有效负载都存在,则系统自动处理通知部分(系统托盘),并且您的应用程序在启动器活动的意图的额外内容中获取数据有效负载(在用户点击通知之后).

为了能够成功地为Android和iOS两个平台提供服务,您可能必须根据客户端的操作系统发送不同的FCM消息.

android Firebase Cloud Messages通知图标

android Firebase Cloud Messages通知图标

如何解决android Firebase Cloud Messages通知图标?

我在项目中使用了FCM,效果很好。但是有一个问题:在某些Android设备上,而不是彩色的应用程序图标(当未设置自定义默认图标且通知有效负载中未设置图标时)显示了一个灰色的圆形。为什么会发生这种情况,我应该使用哪种图标?我应该使用元数据标签设置自定义默认图标吗?

wrong icon

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Android In App Billing:即使成功购买,Receiver的onReceive也永远不会被调用

Android In App Billing:即使成功购买,Receiver的onReceive也永远不会被调用

我不确定要放在哪里的代码,但我在我的应用程序中有一个正在进行的应用程序内购买设置(从某种意义上说,正确的金额和项目已计入我的帐户)但问题是更改从未显示在用户结束时,当他们购买物品时,接收者永远不会得到广播,这将允许我运行在本地保存购买的代码.

我在这里复制了大部分代码:

Simple in app billing Payment

所以我不知道问题是什么.没有什么可以冻结,所有的工作,只是从来没有运行BillingReceiver的onReceive()……其他人有这个吗?

解决方法

需要更多信息来给出具体答案.我没有收到消息,因为我在< / application>之后放置了我的接收节点关闭节点.我应该把它放在< application>之间. < /应用程序>标签.

<application android:icon blah blah...>
    <activity android:name="MyActivity">
    </activity>

    <service android:name=".BillingService" />
    <receiver android:name=".BillingReceiver">
          <intent-filter>
                <action android:name="com.android.vending.billing.IN_APP_NOTIFY" />
                <action android:name="com.android.vending.billing.RESPONSE_CODE" />
                <action android:name="com.android.vending.billing.PURCHASE_STATE_CHANGED" />         
          </intent-filter>
    </receiver> 
</application>

Android Mobile / Wear App onMessageReceived为一条消息调用多次

Android Mobile / Wear App onMessageReceived为一条消息调用多次

我有一个基本的Android Wear应用程序,它成功地使用可穿戴数据层在移动设备和Wear之间发送和接收消息.

但是,无论何时我发送消息(来自Mobile或Wear设备),另一端都会多次接收消息(例如5-17之间的任何地方).为什么不只是一次收到?据我所知,它只发送一次.

成功连接后,这是我发送消息的代码(两端都相同):

private void sendMsgToWear(final String strMsg){

    new AsyncTask<Void, Void, List<Node>>(){

        @Override
        protected List<Node> doInBackground(Void... params) {
            return getNodes();
        }

        @Override
        protected void onPostExecute(List<Node> nodeList) {
            for(Node node : nodeList) {
                Log.v("DEVELOPER", "......Phone: Sending Msg: " + strMsg + " to node:  " + node.getId());

                PendingResult<MessageApi.SendMessageResult> result = Wearable.MessageApi.sendMessage(
                        mGoogleapiclient,
                        node.getId(),
                        strMsg,
                        null
                );

                result.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                    @Override
                    public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                        Log.v("DEVELOPER", "......Phone: " + sendMessageResult.getStatus().getStatusMessage());
                    }
                });
            }
        }
    }.execute();


}

private List<Node> getNodes() {
    List<Node> nodes = new ArrayList<Node>();
    NodeApi.GetConnectednodesResult rawNodes =
            Wearable.NodeApi.getConnectednodes(mGoogleapiclient).await();
    for (Node node : rawNodes.getNodes()) {
        nodes.add(node);
        nodeID = node.getId();
    }
    return nodes;
}

以下是我在我的活动中收到的实现MessageApi.MessageListener的方式:

@Override
public void onMessageReceived(MessageEvent messageEvent) {

    Log.d("DEVELOPER", "......Wear: successfully received phone to wear communication!");


}

我在发送端的输出如下:

07-11 10:54:57.646  26920-28711/com.wearTest.message V/DEVELOPER﹕ ......Phone: Sending Msg: testMessage to node:  6ecc099a-60f8-4c3d-90fe-fa2f248c6bf7
07-11 10:54:57.666  26920-26920/com.wearTest.message V/DEVELOPER﹕ ......Phone: null

接收方记录:

07-11 10:54:57.962    5509-5672/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.962    5509-5610/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.962    5509-5660/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.962    5509-5521/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.962    5509-5539/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972    5509-5570/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972    5509-5604/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972    5509-5601/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972    5509-5566/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972    5509-5520/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972    5509-5582/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972    5509-5567/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.982    5509-5587/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:58.002    5509-5581/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:58.052    5509-5581/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:58.052    5509-5587/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:58.062    5509-5570/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!

提前致谢!

解决方法:

MessageApi应该工作正常.请注意只拨打一次addListener(),否则您会多次收到通知.

或者,添加匹配的removeListener()调用(例如在onResume()/ onPause()中).

android Notification不会触发BroadcastReceiver的onReceive

android Notification不会触发BroadcastReceiver的onReceive

有可能通知开始广播接收者吗?

我试过这个代码,但它没有工作.

通知是创建的,但是当我点击它没有任何反应.

注意:当我将notificationIntent更改为从MybroadcastReceiver.class指向一个活动(如MainActivity.class)时,它工作正常.

通知创建:

notificationmanager notificationmanager = (notificationmanager) context.getSystemService(
        Context.NOTIFICATION_SERVICE);

    int notificationIconId = XXXXXX
    Notification notification = new Notification(
        notificationIconId,XXXXXX,System.currentTimeMillis()
    );

    CharSequence contentTitle = XXXXXXX
    CharSequence contentText = XXXXXX

    Intent notificationIntent = new Intent(context,MybroadcastReceiver.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context,notificationIntent,0);

    notification.setLatestEventInfo(context,contentTitle,contentText,contentIntent);
    notificationmanager.notify(1,notification);

这是broadcastReceiver

public static class MybroadcastReceiver extends broadcastReceiver {

@Override
public void onReceive(Context context,Intent intent) {
   /*
          */

 }
}

在AndroidManifest.xml中

<receiver android:name=".MybroadcastReceiver" />

解决方法

从你的代码…
PendingIntent contentIntent = PendingIntent.getActivity(context,0);

当创建一个定向broadcastReceiver的PendingIntent时,必须使用getbroadcast(…)而不是getActivity(…).

见PendingIntent.getBroadcast(Context context,int requestCode,Intent intent,int flags)

另外,不要这样创建你的意图…

Intent notificationIntent = new Intent(context,MybroadcastReceiver.class);

这是一个明确的Intent,它定位一个特定的类(通常用于启动一个特定的Activity类).

相反,创建一个’广播’意图与’动作’,如…

Intent notificationIntent = new Intent(MyApp.ACTION_DO_SOMETHING);

您还需要指定< intent-filter> < receiver android:name =“.MybroadcastReceiver”/>您的清单部分.

关于android – 当app在后台时,不会调用Firebase onMessageReceived的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于android Firebase Cloud Messages通知图标、Android In App Billing:即使成功购买,Receiver的onReceive也永远不会被调用、Android Mobile / Wear App onMessageReceived为一条消息调用多次、android Notification不会触发BroadcastReceiver的onReceive等相关内容,可以在本站寻找。

本文标签: