繁体   English   中英

在打开推送通知时启动片段吗?

[英]Start fragment on opening Push Notification?

  • 现在,我正在使用parse.com使用我的android应用将推送通知发送给用户。

  • 我能够收到通知,点击应用程序即可正常启动。 我正在使用扩展ParsePushBroadCastReceiver的CustomBroadCastReceiver。

这是Receiver的代码:

package com.myapp;

import android.content.Context;
import android.content.Intent;

import com.parse.ParsePushBroadcastReceiver;

public class Receiver extends ParsePushBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }

    @Override
    protected void onPushReceive(Context context, Intent intent) {
        super.onPushReceive(context, intent);
    }

    @Override
    protected void onPushDismiss(Context context, Intent intent) {
        super.onPushDismiss(context, intent);
    }

    @Override
    protected void onPushOpen(Context context, Intent intent) {
        super.onPushOpen(context,intent);
    }
} 
  • 我正在尝试调用onPushOpen时打开WhatsNew片段。 但是问题是我正在为我的应用程序使用Navigationdrawer。 每当我单击通知应用程序登陆到启动活动时,MainActivity的代码如下:

     package com.myapp; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import com.parse.ParseAnalytics; import com.parse.ParseInstallation; public class MainActivity extends AppCompatActivity implements NavigationDrawerCallbacks { /** * Fragment managing the behaviors, interactions and presentation of the navigation drawer. */ private NavigationDrawerFragment mNavigationDrawerFragment; private Toolbar mToolbar; private static final String TAG = "MyActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); setSupportActionBar(mToolbar); mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer); // Set up the drawer. mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar); ParseAnalytics.trackAppOpenedInBackground(getIntent()); ParseInstallation.getCurrentInstallation().saveInBackground(); } @Override public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments Fragment fragment; FragmentManager fragmentManager = getFragmentManager(); // For AppCompat use getSupportFragmentManager switch (position) { default: case 0: fragment = new Authentication();//search property break; case 1: fragment = new Home();//post property break; case 2: fragment = new PostRequirement();//post requirement break; case 3: fragment = new Properties();//popular locations keep it blank break; case 4: fragment = new Blog();//blog break; case 5: fragment = new Contact();//contact break; case 6: fragment = new NewFragment(); break; } fragmentManager.beginTransaction().replace(R.id.container, fragment).commit(); } @Override public void onBackPressed() { if (mNavigationDrawerFragment.isDrawerOpen()) mNavigationDrawerFragment.closeDrawer(); else super.onBackPressed(); } } 
  • 在打开推送通知应用程序时,它将到达Authentication Fragment 我想要做的是每当用户通过推送通知打开应用程序时打开该NewFragment

该应用程序的清单文件:

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

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true"
        android:xlargeScreens="true" />

    <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.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <permission
        android:name="com.myapp.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.myapp.permission.C2D_MESSAGE" />

    <application
        android:name=".ParseApplication"
        android:allowBackup="true"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.parse.push.notification_icon"
            android:resource="@mipmap/ic_launcher" />

        <activity
            android:screenOrientation="portrait"
            android:name=".MainActivity"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name="com.myapp.NoConnection"
            android:configChanges="orientation"
            android:label="@string/app_name"
            android:screenOrientation="portrait"></activity>


        <service android:name="com.parse.PushService" />

        <receiver
            android:name="com.myapp.Receiver"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <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>
        <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.myapp" />
            </intent-filter>
        </receiver>

    </application>


</manifest>
  • 我对此问题进行了大量研究,但没有得到满意的答案。 无法使用Intent来调用片段,因此无法在OnPushOpen方法中传递Intent

  • 我应该怎么做,以便每当用户单击推送通知应用程序时,它将落在NewFragment而不是Authentication Fragment上。

  • 感谢您花很长时间阅读问题! 但是我真的很讨厌这个东西。

抱歉,收到推送通知时无法启动片段。 相反,您可以启动一个新活动,该活动可以在正确的屏幕/布局上显示新的通知。

为此,转到您的ParseApplication.java

public class ParseApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Parse.initialize(this, keys.applicationId, keys.clientKey);
        PushService.setDefaultPushCallback(this, NewActivity.java);
        ParseInstallation.getCurrentInstallation().saveEventually();
    }

基本上,当您收到通知并且用户单击时,它将加载NewActivity.java ,这将依次打开xml文件。

现在在NewActivity.java转到setContentView onCreate

setContentView(R.layout.**layout name**);

把你的XML文件。

如果要保留发送的推送通知,请使用共享首选项。

您应该从YourBroadCastReceiver向捆绑中添加一个参数,指示转到新的Fragment,然后从活动内部转到该Fragment,检查给定捆绑中的给定参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM