如何自定义解析推送通知在Android应用程序中发送uri?

eli*_*eli 1 java notifications android push

我想在我的Android应用程序中使用解析推送通知功能.我把我的解析推送通知代码放在我的第一个活动(启动活动)中.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import com.parse.ParseAnalytics;
import com.parse.ParseInstallation;
import com.parse.PushService;

public class SplashActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    ParseAnalytics.trackAppOpened(getIntent());

    // inform the Parse Cloud that it is ready for notifications
    PushService.setDefaultPushCallback(this, HomeActivity.class);
    ParseInstallation.getCurrentInstallation().saveInBackground();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
            startActivity(intent);
            SplashActivity.this.finish();
        }
    }, 1000);
}
public void onBackPressed() {

}
}
Run Code Online (Sandbox Code Playgroud)

这是我的ParseApplication:

import com.parse.Parse;
import com.parse.ParseACL;

import com.parse.ParseUser;

import android.app.Application;

public class ParseApplication extends Application {

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

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


    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);
}

 }
Run Code Online (Sandbox Code Playgroud)

这是我的清单:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:name="ParseApplication"
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
    <activity
        android:name=".SplashActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

     <activity android:name=".HomeActivity" ></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>
</application>
Run Code Online (Sandbox Code Playgroud)

现在我可以轻松地在应用程序中使用json发送推送通知,如下所示:

   {"title" : "my title",
    "alert" : " my alert text",
    "uri" :"http://Google.com"
    }
Run Code Online (Sandbox Code Playgroud)

在这里,我可以轻松获得我的头衔和我的警报,但问题是,当我点击它并进入我的家庭活动时,它根本不是我的uri.我的问题是什么?我想打开我的uri.

Fra*_*lin 10

您需要在URI字段中添加活动的名称.
例如:

.SplashActivity

就我而言,我们有这个清单:

...
<activity
        android:name=".presenter.FooActivity"
        android:label="@string/title_activity_foo"
        android:parentActivityName=".presenter.fooParent">
<intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:scheme="myapp" android:host="host" android:path="/path" />
        </intent-filter>
</activity>
...
Run Code Online (Sandbox Code Playgroud)

使用Parse的Push的json是:

{"title" : "my title",
"alert" : " my alert text",
"uri" :"myapp://host/path"
}
Run Code Online (Sandbox Code Playgroud)

如果您要打开http://www.google.com,则可能需要在活动上接收一些代码,并创建一个新代码以转到该网址.为此,您需要为json添加自定义属性以传递所需的URL.就像是:

{"title" : "my title",
"alert" : " my alert text",
"uri" :"myapp://host/path",
"customurl":"http://www.google.com"
}
Run Code Online (Sandbox Code Playgroud)

和代码:

Bundle params = intent.getExtras();
if (params!=null)
{
  String param = params.getString("com.parse.Data");
  String url = funcThatGetsUrlFromData(param);
  Intent i = new Intent(Intent.ACTION_VIEW);
  i.setData(Uri.parse(url));
  startActivity(i);
 }
Run Code Online (Sandbox Code Playgroud)