无法在通知点击时弹出新活动

时间:2017-12-09 07:50:55

标签: android android-intent android-activity android-notifications

所以我想让这个应用程序接收SMS文本,当它发生时,它会显示一个通知,当用户点击它时,会弹出一条带有SMS消息的新活动。因此,如果它像接收SMS文本的BroadcastReceiver类一样完成,并且它显示一个非常精细和花花公子的通知,那么我最多。

public class SMSReceiver extends BroadcastReceiver {

    static final int notificationId = 1;

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

        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String message = currentMessage.getDisplayMessageBody();
                    newNotification(context, phoneNumber, message);

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void newNotification(Context context, String title, String text) {

        NotificationManager notifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notifyIntent = new Intent(context, NotificationActivity.class);
        notifyIntent.putExtra("SMSText", text);
        notifyIntent.setAction(text);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notifyIntent, 0);

        Notification notification = new NotificationCompat.Builder(context)
                .setContentIntent(contentIntent)
                .setContentText(text)
                .setContentTitle(title)
                .setTicker(text)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .build();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notifyMgr.notify(notificationId, notification);

    }

}

唯一的问题是当我点击通知时。第一次完美运行时,会弹出NotificationActivity类,显示SMS消息。但在那之后,信息永远不会更新。我假设应用程序意识到它已经打开NotificationActivity,因此它不会打开另一个。即使我在创建新通知时正确添加了标记FLAG_ACTIVITY_NEW_TASK。这里还有我的NotificationActivity类

public class NotificationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        String newString;
        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if (extras == null) {
                newString = null;
            } else {
                newString = extras.getString("SMSText");
            }
        } else {
            newString = (String) savedInstanceState.getSerializable("SMSText");
        }

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(newString);

    }
}

我也有一个启动器活动,MainActivity,但觉得没有必要显示,因为它只是注册SMSReceiver。非常感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

您可以尝试重写方法onNewIntent,只要将新Intent发送到同一Activity,就会使用getIntent方法保留原始意图,只要有新的Intent,就会调用这个。

@Override
protected void onNewIntent(Intent intent) 
{
    //Do what you want to do with this new Intent here!
}

答案 1 :(得分:0)

更改此行

    PendingIntent contentIntent = PendingIntent.getActivity(context, 
    notificationId , 
    notifyIntent, PendingIntent.FLAG_ONE_SHOT););