Pushwoosh集成-原生系统推送

本文介绍了如何在Android应用中集成Pushwoosh推送服务,包括添加依赖、自定义通知样式和处理点击通知后的跳转逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Pushwoosh是境外的一个提供免费推送服务的公司,Android app当然也是基于Google Cloud Messaging 封装的。

因为官方文档不太直观,也可能是之前不太了解过国外此类第三方服务的套路,使用过程也遇到了一些小问题,算是采坑了吧!

添加依赖

compile 'com.pushwoosh:pushwoosh:+'
compile 'com.android.support:support-v4:23.1.1+'
compile 'com.google.android.gms:play-services-gcm:8.4.0+'
compile 'com.google.android.gms:play-services-location:8.4.0+'

如果不使用Geozones push的话就不需要添加location依赖了。

XML

在AndroidManifest.xml中application节点下添加:

<meta-data android:name="PW_APPID" android:value="XXXXX-XXXXX" />
<meta-data android:name="PW_PROJECT_ID" android:value="A123456789012" />

PW_APPID是在Pushwoosh创建应用的ID
PW_PROJECT_ID是从Google开发者控制台设置GCM拿来的工程号,
notice: 你需要给这个工程号手动添加前缀“A”。

NotificationFactory

自定义NotificationFactory继承DefaultNotificationFactory,实现自己的通知样式。

public class CustomContentNotificationFactory extends DefaultNotificationFactory {

    @Override
    public Notification onGenerateNotification(PushData pushData) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            return super.onGenerateNotification(pushData);
        }
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext())
                .setContentTitle(getContentFromHtml(pushData.getHeader()))
                .setContentText(getContentFromHtml(pushData.getMessage()))
                .setSmallIcon(R.drawable.ic_notification_small)
                .setLargeIcon(BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_launcher))
                .setTicker(getContentFromHtml(pushData.getTicker()))
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_HIGH);

        final Notification notification = notificationBuilder.build();
        addSound(notification, pushData.getSound());
        addVibration(notification, pushData.getVibration());
        addCancel(notification);

        return notification;
    }

}

这一步定义了通知的样式,这样我们收到推送后就会显示自定义的通知样式及数据,但一般我们需要在点击通知后跳转到app指定的页面,这里大家就不要想着给通知添加PendingIntent了,当然添加7.0的Action是可以的。

PushReceiver

  • 自定义PushReceiver继承BroadcastReceiver,这样收到推送弹出通知,点击通知后就会进入PushReceiver的onReceive(),在这里实现跳转逻辑。

    public class PushReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null)
            return;
        Bundle bundle = PushManagerImpl.preHandlePush(context, intent);
        if (bundle == null)
            return;
        String type = bundle.getString(Constants.TYPE);
        String id = bundle.getString(Constants.ID);
        String sn = bundle.getString(Constants.SN);
    
        if (TextUtils.isEmpty(type))
            return;
        switch (type) {
    
            case Constants.PUSH_SCENE:
                if (!TextUtils.isEmpty(id) && !TextUtils.equals("0", id))
                    intent = SceneActivity.newIntent(context, new Scene().setId(Integer.parseInt(id)));
                break;
            case Constants.PUSH_ORDER:
                intent = new Intent(context, OrderActivity.class);
                intent.putExtra(Constants.SN, sn);
                break;
            case Constants.PUSH_SHOP:
                intent = new Intent(context, IndexActivity.class);
                intent.putExtra(Constants.FROM_PUSH,true);
                break;
            case Constants.PUSH_CATALOG:
                intent = defaultIntent(context,new Catalog().setId(Integer.parseInt(id)));
                break;
        }
        if (intent == null)
            return;
        Intent mainIntent = new Intent(context, IndexActivity.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Intent[] intents = null;
        if (SystemUtils.isAppAlive(context, Constants.PACKAGE_NAME)) {
            LogUtils.logd("the app process is alive");
            intents = new Intent[]{mainIntent,intent};
        } else {
            LogUtils.logd("the app process is dead");
            Intent launchIntent = context.getPackageManager().
                    getLaunchIntentForPackage(Constants.PACKAGE_NAME);
            launchIntent.setFlags(
                    Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            intents = new Intent[]{launchIntent, mainIntent, intent};
        }
        context.startActivities(intents);
    }
    

    }

注意:

Bundle bundle = PushManagerImpl.preHandlePush(context, intent);

这行代码千万别少了,如果直接从 intent.getExtras(); 拿到的bundle对象是没有数据的。
如果要从bundle中拿到json格式数据,控制台在发送消息时,在root params中要按照下面格式:

 Android root params example : { "my_actions" : [ { "title" : "Pushwoosh", "url" : "https://www.pushwoosh.com"  } ] }

然后通过 String actions = bundle.getString(“my_actions”);拿到数据。

  • 别忘了在xml中配置:


ps: 如果你没有定义PushReceiver的话,之前的自定义通知点击后默认跳入app主页,无论app进程是否还在,都会走一遍Splash页的。

推送服务注册

在Application的onCreate()中:

    final PushManager pushManager = PushManager.getInstance(context);

        pushManager.setNotificationFactory(new CustomContentNotificationFactory());
        try {
            pushManager.onStartup(context);
        } catch (Exception e) {
            Log.e("Pushwoosh", e.getLocalizedMessage());
        }
        //Register for push!
        pushManager.registerForPushNotifications();
ps:
-keep class com.pushwoosh.** { *; }
-keep class com.arellomobile.** { *; }
-dontwarn com.pushwoosh.**
-dontwarn com.arellomobile.**

That’s all !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值