Android Firebase 推送通知空白(无文本)

标签 android firebase notifications push firebase-cloud-messaging

我的 Android 应用程序推送通知有问题。 我的推送通知是空白的,只有应用名称可见。消息字段为空。

image http://www.angelweb.in/snapshot.png .

Androidmanifest.xml

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="in.myappname.apk" >
        <!-- GCM connects to Google Services. -->
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <!-- GCM requires a Google account. -->
        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <permission android:name="in.myappname.apk.permission.C2D_MESSAGE" android:protectionLevel="signature" />
        <uses-permission android:name="in.myappname.apk.permission.C2D_MESSAGE" />
        <!-- This app has permission to register and receive dataf message. -->
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <intent-filter>
                    <action android:name="in.myappname.apk.MESSAGE" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
            <receiver
                android:name="com.pushbots.google.gcm.GCMBroadcastReceiver"
                android:permission="com.google.android.c2dm.permission.SEND" >
                <intent-filter>
                    <!-- Receives the actual messages. -->
                    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                    <!-- Receives the registration id. -->
                    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                    <category android:name="in.myappname.apk" />
                </intent-filter>
            </receiver>
            <receiver android:name="com.pushbots.push.DefaultPushHandler" />
            <service android:name="com.pushbots.push.GCMIntentService" />
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>

        </application>
    </manifest>

主 Activity .java

package in.myappname.apk;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.pushbots.push.Pushbots;

public class MainActivity extends Activity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Pushbots.sharedInstance().init(this);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);

        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Use remote resource
        mWebView.loadUrl("http://myappurl.in");

        // Stop local links and redirects from opening in browser instead of WebView
        mWebView.setWebViewClient(new MyAppWebViewClient());

        // Use local resource
        // mWebView.loadUrl("file:///android_asset/www/index.html");
    }

    // Prevent the back-button from closing the app
    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle("Alert")
                    .setMessage("Are you sure you want exit?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }

                    })
                    .setNegativeButton("No", null)
                    .show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

MyAppWebViewClient.java

package in.myappnam.apk;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyAppWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().endsWith("myappname.in")) {
            return false;
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}

布局-->activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="in.myappname.apk.MainActivity">

        <WebView
            android:id="@+id/activity_main_webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>

值 -> pushbots.xml

 <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!-- Pushbots Application ID  -->
        <string name="pb_appid">xxxxxxxxxMypushbotskey</string>
        <!-- Project Number in Google console -->
        <string name="pb_senderid">xxxxxxxxxxMysenderid</string>
        <!-- Pushbots Log Level  log Tag "PB2" -->
        <string name="pb_logLevel">DEBUG</string>
    </resources>

值 -> strings.xml

 <?xml version="1.0" encoding="utf-8"?>
    <resources>

        <string name="app_name">My App Name</string>
        <string name="hello_world">My Alternative tag</string>
        <string name="action_settings">Settings</string>

    </resources>

最佳答案

改变

 <service android:name="com.pushbots.push.GCMIntentService" />
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>

<service android:name="com.pushbots.push.GCMIntentService" >
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
 </service>

请发布您的 GCMBroadcastReceiver 类

关于Android Firebase 推送通知空白(无文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38169396/

相关文章:

android - 使 READ、WRITE 和 NOTIFY 在 Android BLE(版本 21 及更高版本)上工作

javascript - 如何使用 Google Analytics 确定客户所在的国家/地区?

java - JSONObject.put(String) 为每个 '\' 添加 '/'

android - 清理 ActionBarSherlock 创建 R cannot be resolved 错误

javascript - Firebase 数据库没有响应

javascript - FCM : firebase getToken options( ServiceWorkerRegistration, vapidKey)

android - 在 map 上绘制新圆之前先删除先前的圆

android - 如何在 Android Studio 中为 'Fastfile' 指定编程语言?

ios - 推特登录异常 flutter

javascript - Firebase 函数 FCM