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

时间:2016-07-03 11:15:32

标签: 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>

MainActivity.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;
    }
}

布局 - &gt; 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>

值 - &gt; 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>

值 - &gt;的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>

1 个答案:

答案 0 :(得分:0)

更改

 <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 Class