Question

I have an application where the user may send recipes to others with the application. Right now, the app is able to send the notification, however, when the notification is clicked, it does not open the intended activity (SmsViewActivity) it simply closes the drawer.

Here is the code for the SmsViewActivity:

public class SmsViewActivity extends Activity {
    private static final String TAG = "SmsViewActivity";
    private static final String SMS_FOOD = "food_recieved";
    private FoodJSONSerializer mSerializer;
    public Button mSaveButton, mDismissButton;
    public int mNotificationId;
    public String message;
    private EditText mTitleField;
    private CheckBox mImperialCheckBox;
    private CheckBox mMetricCheckBox;
    private EditText mServingsField;
    private EditText mDirectionsField;
    private Spinner mMetricSpinner;
    private Spinner mImperialSpinner;
    Food mFood;
    private String msg;
    private Activity mActivity;

    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        mActivity = this;
        setContentView(R.layout.sms_view);
        mSaveButton = (Button) findViewById(R.id.save_button_sms);
        mDismissButton = (Button) findViewById(R.id.dismiss_button_sms);

        this.msg = getIntent().getStringExtra("message");
        try {
            JSONObject jsonRecipe = new JSONObject(this.msg);
            this.mFood = new Food(jsonRecipe);

            Log.i(TAG, "Food = " + mFood);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        mNotificationId = getIntent().getIntExtra("notificationid", 0);
        if (mNotificationId == 0) {
            Log.e(TAG, "Could not retrieve notification ID.");
            Toast.makeText(this, "A fatal error has occurred in SMS viewer.",
                    Toast.LENGTH_LONG).show();
            finish();
        }

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager notificationMgr = (NotificationManager) getSystemService(ns);
        notificationMgr.cancel(mNotificationId);

        this.mTitleField = (EditText) findViewById(R.id.food_title_sms);
        this.mTitleField.setText(mFood.getTitle());

        this.mServingsField = (EditText) findViewById(R.id.food_servings_sms);
        this.mServingsField.setText(Integer.toString(mFood.getServings()));

        this.mDirectionsField = (EditText) findViewById(R.id.directions_text_sms);
        this.mDirectionsField.setText(mFood.getDirections());

        this.mSaveButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                FoodStorage.get(mActivity).addFood(mFood);
                finish();
            }
        });

        this.mDismissButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                backToList();
                finish();
            }
        });
    }

    public void backToList() {
        Intent i = new Intent(this, FoodListFragment.class);
        startActivity(i);
    }
}

Also here is the receiver Activity:

@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;

    Bundle bundle = intent.getExtras();
    Map<String, String> msgTexts = new LinkedHashMap<String, String>();

    if (bundle != null){
        Object[] pdus = (Object[]) bundle.get("pdus");

        for (Object pdu : pdus) {
            SmsMessage  message    = SmsMessage.createFromPdu((byte[]) pdu);
            String   incomingMsg   = message.getMessageBody();
            String   originatingNumber  = message.getOriginatingAddress();
            Log.i(TAG, "Rcvd: from " + originatingNumber + " Msg: " + incomingMsg);
            msgTexts.put(originatingNumber, incomingMsg);
            smsMessage = incomingMsg;
        }
    }
    processMessages(msgTexts, mNextNotificationId);
}

public void processMessages( Map<String, String> msgTexts, int notificationId ){
    for (Entry<String, String> entry : msgTexts.entrySet()) {
        Log.i(TAG, "Processing message: " + entry.getValue());

        Intent notificationIntent = new Intent(mContext, SmsViewActivity.class);
        notificationIntent.putExtra("message", entry.getValue());
        notificationIntent.putExtra("sender", entry.getKey());
        notificationIntent.putExtra("notificationid", notificationId);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder = new NotificationCompat.Builder(mContext)
                .setSmallIcon(R.drawable.ic_launcher)
                .setTicker("Recipe Received")
                .setContentTitle("Recipe Received")
                .setContentIntent(resultPendingIntent)
                .setContentText("from " + entry.getKey())
                .setWhen(System.currentTimeMillis());

        NotificationManager notificationMgr = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationMgr.notify(notificationId++, mBuilder.build());
    }
}
Was it helpful?

Solution

Try changing the intent flag to: PendingIntent.FLAG_CANCEL_CURRENT

Otherwise it just updates the existing intent. It should be possible to have an onNewIntent handler in your activity, but I found this works more reliably.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top