无法获得推送通知文本

时间:2014-02-28 10:41:40

标签: java android push-notification parse-platform

我创建了一个示例Android应用程序,用于接收来自 Parse 的推送消息。我跟着this video tutorial。该应用程序运行正常,我能够接收推送通知。

所需的课程如下:

ParseApplication 类:

public class ParseApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        // Add your initialization code here
        Parse.initialize(this, "ygdC4y3uzCe0TOEPOfB1U469Gg5ZJGxF2OGlNKCG", "Tk5ZlDiWWVJm2Xio5IiXk0KI5JM1jrvGPOSpFxzE");

        PushService.setDefaultPushCallback(ParseApplication.this, SampleClass.class);



        ParseUser.enableAutomaticUser();
        ParseACL defaultACL = new ParseACL();

        // If you would like all objects to be private by default, remove this line.
        defaultACL.setPublicReadAccess(true);

        ParseACL.setDefaultACL(defaultACL, true);
    }

}

ParseStarterProjectActivity 类:

public class ParseStarterProjectActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ParseAnalytics.trackAppOpened(getIntent());


    }
}

SampleClass 类:

public class SampleClass extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_class);
    }

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

}

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.parse.starter"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <!--
  IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
  to match your app's package name + ".permission.C2D_MESSAGE".


    -->
    <permission
        android:name="com.parse.starter.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.parse.starter.permission.C2D_MESSAGE" />

    <application
        android:name="com.parse.starter.ParseApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.parse.starter.ParseStarterProjectActivity"
            android:label="Test Push" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.parse.starter.MainActivity"
            android:label="Test Push" >
        </activity>

        <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.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" />

                <!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
                <category android:name="com.parse.starter" />
            </intent-filter>
        </receiver>

        <activity
            android:name="com.parse.starter.SampleClass"
            android:label="@string/title_activity_sample_class" >
        </activity>
    </application>

</manifest>

现在,我希望在用户收到推送通知时从其中获取文本。如何从通知中获取文本?

这是推送通知服务器的url

我甚至去了this

1 个答案:

答案 0 :(得分:0)

我必须像这样更改 SampleClass Activity

public class SampleClass extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_class);

        Intent intent = getIntent(); 
        Bundle extras = intent.getExtras(); 
        String jsonData = extras.getString( "com.parse.Data" );

        Log.i("Data Received:", jsonData);

    }

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

其余的就是这样。我提到this link