Android开发Android开发Android开发经验谈

Android 接收推送消息跳转到指定页面

2018-01-10  本文已影响1269人  AntDream

问题的提出

实现思路

App在前台时,弹出Dialog样式的Activity

<style name="AlertDialogActivityTheme" parent="Theme.AppCompat.Dialog">
    <item name="windowActionBar">false</item>
    <item name="android:windowFrame">@null</item>
    <item name="windowNoTitle">true</item> //去掉标题
    <item name="android:windowBackground">@android:color/transparent</item> //背景透明
    <item name="android:windowCloseOnTouchOutside">true</item> //设置触摸弹框外面是否会消失
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>
</style>
#AndroidManifest.xml
<activity
    android:name=".getui.AlertDialogActivity"
    android:theme="@style/AlertDialogActivityTheme">
</activity>
<item name="android:windowNoTitle">true</item>
public class AlertDialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_activity);
        
        //设置弹框大小,此处宽度为屏幕宽度减去160像素
        getWindow().setLayout(DeviceUtil.getDisplayParametersW(this)-160, ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.CENTER);

        initView();
    }
}

App在后台或是已经被销毁

//关键的地方
PendingIntent contentIntent = PendingIntent.getActivities(context, 0, intents, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
        //省略其他的一些设置
        .setContentIntent(contentIntent)
        //省略其他的一些设置
        
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify((int) System.currentTimeMillis() / 1000, notification);
Intent[] intents = new Intent[2];
Intent intent_main = new Intent(getApplicationContext(), MainActivity.class);
Intent intent_target = new Intent(getApplicationContext(), TargetActivity.class);

intents[0] = intent_main;
intents[1] = intent_target;
Intent intent_main = new Intent(getApplicationContext(), MainActivity.class);
Intent[] intents = new Intent[1];
intents[0] = intent_main;
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Intent.migrateExtraStreamToClipData()' on a null object reference

以上就是接收推送消息后的跳转的一些内容,希望能帮到大家


补充另外一种实现方式,主要是用到了TaskStackBuilder

首先需要为跳转的目标Activity在AndroidManifest文件中设置一个属性parentActivityName,表示跳转的Activity退出后显示的Activity,一般都是MainActivity

<activity 
    android:name="com.xxq2dream.service.JumpActivity"
    android:parentActivityName=".MainActivity">

然后在创建通知的地方修改一下PendingIntent相关的代码

private void showNotification() {
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    Intent msgIntent = new Intent();
    msgIntent.setClass(this, JumpActivity.class);

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
    taskStackBuilder.addParentStack(JumpActivity.class);
    taskStackBuilder.addNextIntent(msgIntent);
    PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    
    ...
}

这样也能达到上面使用Intent数组来创建PendingIntent的效果。这种用法适合在组件化开发中。


今天你进步了嘛?欢迎关注我的微信公众号,和我一起每天进步一点点!


AntDream
上一篇下一篇

猜你喜欢

热点阅读