通知不会打开活动

时间:2014-05-02 18:32:03

标签: java android notifications

我有一个应用程序,用户可以通过该应用程序向他人发送食谱。 现在,该应用程序可以发送通知,但是,当单击通知时,它不会打开预期的活动(SmsViewActivity)它只是关闭抽屉。

以下是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);
    }
}

此处还有接收器活动:

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

1 个答案:

答案 0 :(得分:0)

尝试将intent标志更改为:     PendingIntent.FLAG_CANCEL_CURRENT

否则它只是更新现有的意图。应该可以在你的活动中有一个onNewIntent处理程序,但我发现它更可靠。