简体   繁体   English

通知将不会打开

[英]Notification will not open Activity

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. 现在,该应用程序能够发送通知,但是,单击通知时,它不会打开预期的活动(SmsViewActivity),而只是关闭了抽屉。

Here is the code for the 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);
    }
}

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

Try changing the intent flag to: PendingIntent.FLAG_CANCEL_CURRENT 尝试将意图标志更改为: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. 您的活动中应该可以有一个onNewIntent处理函数,但是我发现这更可靠。

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

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