解析Android上的通知将无法正常工作

前端之家收集整理的这篇文章主要介绍了解析Android上的通知将无法正常工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在 Android上设置Parse推送通知一段时间.
我遵循不同的教程,我正在尝试从他们的网络平台发送通知,但似乎没有任何效果.这是我迄今为止所尝试的.

这是我的Application类的onCreate方法

@Override
    public void onCreate() {
        super.onCreate();
        Log.i("PushNotifications","PARSE initialize");

        Parse.initialize(this,"******************************","*****************************");
        ParseInstallation.getCurrentInstallation().saveInBackground();

        ParsePush.subscribeInBackground("",new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    Log.i("PushNotifications","com.parse.push" + " successfully subscribed to the broadcast channel.");
                } else {
                    Log.i("PushNotifications","com.parse.push" + " Failed to subscribe for push");
                }
            }
        });

    }

这被称为成功,因为我得到日志

successfully subscribed to the broadcast channel.

这里还有我的清单文件的一些相关内容

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<permission android:name="com.my.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.my.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name=".App"
    android:theme="@style/AppTheme">

    <activity
        android:name=".activities.MainActivity"
        android:launchMode="standard"
        android:screenOrientation="sensorPortrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <Meta-data android:name="com.parse.push.gcm_sender_id" android:value="id:123456789" />
    <Meta-data android:name="com.parse.push.notification_icon" android:resource="@drawable/ic_launcher"/>

    <service android:name="com.parse.PushService" />
    <receiver android:name="com.parse.ParseBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>
    <receiver android:name="com.parse.ParsePushBroadcastReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.DELETE" />
            <action android:name="com.parse.push.intent.OPEN" />
        </intent-filter>
    </receiver>
    <!--com.parse.GcmBroadcastReceiver"-->
    <receiver android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.my.app" />
        </intent-filter>
    </receiver>

</application>

当我尝试从Parse网站发送推送,没有任何反应.

由于这不起作用,我尝试实现我自己的GcmBroadcastReceiver并在清单中更改它.

<receiver android:name=".receivers.MyCustomReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.my.app" />
            </intent-filter>
</receiver>

public class MyCustomReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context,Intent intent) {
        Log.i("PushNotifications","MyCustomReceiver");
    }
}

没有成功.

然后我也尝试创建我自己的ParsePushBroadcastReceiver(通过从ParsePushBroadcastReceiver继承).

<receiver android:name=".receivers.CustomParseReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
            </intent-filter>
 </receiver>

public class CustomParseReceiver extends ParsePushBroadcastReceiver {

   @Override
    protected Notification getNotification(Context context,"PARSE getNotification");
        return null;
    }

    @Override
    protected void onPushOpen(Context context,"PARSE onPushOpen");
    }

    @Override
    protected void onPushReceive(Context context,"PARSE onPushReceive");        
    }

    private JSONObject getDataFromIntent(Intent intent) {
        Log.i("PushNotifications","PARSE getDataFromIntent");            
    }

}

它也没有工作.

事情是..当我创建自己的GCM BroadcastReceiver并且我从我自己的服务器发送通知(有一些小的PHP脚本),通知被成功接收.

我真的很想知道我实施客户端解析通知系统有什么问题.

任何暗示问题可能来自哪里?

解决方法

我的2美分(希望这有帮助):

我有一个类似的问题,我解决了(解释为Here),所以这可能是问题,如果没有,看看我的文件,匆匆一瞥,他们似乎有一些小的差异,他们的工作,所以它是值得一看:

清单部分:

<!-- for parse pushes -->
    <service android:name="com.parse.PushService" />

    <receiver android:name="com.parse.ParseBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>
    <receiver
            android:name="com.MyStuff.stuff.utils.MyPushReceiver"
            android:exported="false" >
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.OPEN" />
            <action android:name="com.parse.push.intent.DELETE" />
        </intent-filter>
    </receiver>
    <receiver
            android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.MyStuff.stuff" />
        </intent-filter>
    </receiver>
    <Meta-data
            android:name="com.parse.push.notification_icon"
            android:resource="@drawable/ic_launcher" />

我实现了我自己的ParsePushBroadcastReceiver:

public class MyPushReceiver extends ParsePushBroadcastReceiver
{
    @Override
    protected void onPushOpen(Context context,Intent intent)
    {
     //bla bla...
    }

    @Override
    protected void onPushReceive(Context context,Intent intent)
    {
     // bla bla...
    }

    @Override
    protected Notification getNotification(Context context,Intent intent)
    {
        Notification notification = super.getNotification(context,intent);
     // do some stuff with it..
    }

}

和我的应用程序类:

public class MyApplication extends Application
{

    private static final String CKey = "***********";
    private static final String AId = "************";

    @Override
    public void onCreate()
    {
        super.onCreate();
        Parse.initialize(this,AId,CKey);

        //initialized some stuff..

        ParsePush.subscribeInBackground("",new SaveCallback()
        {
            @Override
            public void done(ParseException e)
            {
                if (null == e)
                {
                    ParseInstallation install = ParseInstallation.getCurrentInstallation();
                    String token = install.getString("deviceToken");
                   //do some stuff with it...
                }
            }
        });
        ParseInstallation.getCurrentInstallation().saveInBackground();
        //some more stuff
    }

希望它让你走上正确的道路!

原文链接:https://www.f2er.com/android/313330.html

猜你在找的Android相关文章