简体   繁体   中英

onNewIntent is not being called

I'm trying pass over push data from receiver to the activity using an Intent and it doesn't seem to work. Any idea why? Here is my implementation:

AndroidManifest.xml

<?xml version="1.0" encoding="UTF-8"?><!--

<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_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />

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


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="RB"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:name="com.appName.appExtension">
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="API_KEY"
        />

    <activity
        android:name=".MainActivity"
        android:label="RB"
        android:theme="@style/AppTheme.NoActionBar"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity android:name="com.appNamw.RouteView"
        android:label="@string/title_activity_route_view"
        android:screenOrientation="landscape">

    </activity>
    <service android:name="com.parse.PushService" />
    <receiver android:name="com.appName.ParseReceiver"
        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>
    <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.appName" />
        </intent-filter>
    </receiver>
</application>

ParseReceiver

package com.appName;

import android.content.Context;
import android.content.Intent;
import android.nfc.Tag;
import android.util.Log;

import com.parse.ParseAnalytics;
import com.parse.ParsePushBroadcastReceiver;

import org.json.JSONException;
import org.json.JSONObject;


public class ParseReceiver extends ParsePushBroadcastReceiver {
    private final String TAG = "Push_Notify";

    @Override
    public void onPushOpen(Context context, Intent intent) {
    Log.i(TAG, "onPushOpen triggered!");

    ParseAnalytics.trackAppOpenedInBackground(intent);
    Intent mainIntent = new Intent(context, MainActivity.class);
    mainIntent.putExtras(intent.getExtras());
    mainIntent.putExtra("method", "updateStatus");

    mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


    JSONObject pushData;

    String alert;
    try {
        pushData = new JSONObject(intent.getStringExtra(ParseSupplyReceiver.KEY_PUSH_DATA));
        alert = pushData.getString("alert");
        String name = pushData.getString("name");
        String contactNumber = pushData.getString("contactNumber");
        String durationText = pushData.getString("duration");

        mainIntent.putExtra("alert", alert);
        mainIntent.putExtra("pushData", new PushData(name, contactNumber, durationText));

    } catch (JSONException e) {
        Log.i(TAG,"JSONException: " + e.getLocalizedMessage());
    }

    context.startActivity(mainIntent);

   }
}

PushData.java

package com.appName;


import android.os.Parcel;
import android.os.Parcelable;


public class PushData implements Parcelable{
   String name;
   String contactNumber;
   String durationText;


   public PushData (String name,String contactNumber,String durationText ){
  /*
   * Reconstruct from the Parcel. Keep same order as in writeToParcel()
   */

       this.name = name;
       this.contactNumber = contactNumber;
       this.durationText = durationText;
  }

  public PushData(Parcel source) {
      name = source.readString();
      contactNumber = source.readString();
      durationText = source.readString();

  }


    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(contactNumber);
        dest.writeString(durationText);
   }

   @Override
   public int describeContents() {
      return 0;
   }

   public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

      public PushData createFromParcel(Parcel source) {

          return new PushData(source);
      }

      public PushData[] newArray(int size) {

          return new PushData[size];
      }

  };
}

Implementation of onNewIntent in MainActivity:

 @Override
protected void onNewIntent(Intent intent){
    super.onNewIntent(intent);
    setIntent(intent);

    if (intent.getStringExtra("method").equals("updateStatus")) {
        alertString = intent.getStringExtra("alert");
        pushData = intent.getParcelableExtra("pushData");
        updateStatus();
    }
}



public void updateStatus() {


    if (pushData != null) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("name", pushData.name);
        editor.putString("contactNumber", pushData.contactNumber);
        editor.putString("duration", pushData.durationText);
    }
}

May I know what is wrong here? I have done an similar implementation on another app and it works completely fine.

将FLAG_ACTIVITY_SINGLE_TOP标志添加到您的意图(请参见onNewIntent(Intent intent) )。

I think it s not about your FLAG,but your activity s launchmode;

It seems like that this method only be called when it`s launchmode is singleTask or sigleTop;

You also have a spelling mistake in your Manifest. Change

<activity android:name="com.appNamw.RouteView"
    android:label="@string/title_activity_route_view"
    android:screenOrientation="landscape">

TO

<activity android:name="com.appName.RouteView"
    android:label="@string/title_activity_route_view"
    android:screenOrientation="landscape">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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