三方

极光推送

2019-09-26  本文已影响0人  小慧sir

[一、百度一下:极光推送的官网]

image

[二、进入官网,并注册账号登录]

image

[三、点击“应用管理”,并“创建应用”]

image

[四、应用名称、应用图标随意起名,上传,最后点击“创建我的应用”]

image

[五、请记好你的AppKey,然后点击“完成推送设置”]

image

[六、我们先测试android,请填入你”项目的包名”,最后点击保存,弹出一个窗口,我们点击确定]

image image

【七、点击下载Demo】

image

【八、下载完成后,建一个文件夹,把压缩包放进去然后解压到当前文件夹】

image image

【九、返回浏览器,点击查看集成指南】

image

【十、我们选择自动集成】

image

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        //把service这段复制,tools报红就导包,name爆红先不管
        <service android:name="cn.jpush.android.service.PushService"
            android:process=":multiprocess"
            tools:node="replace" >
        </service>

    </application>

image

【十一、Sync Now 等运行完毕就OK咯】

【十二、开始推送(以上为自动集成步骤)】


 <receiver
            android:name=".di.global.PushReceiver" //换成自己的receiver路径!!!
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="cn.jpush.android.intent.REGISTRATION" />
                <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
                <action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" />
                <action android:name="cn.jpush.android.intent.CONNECTION" />
                <category android:name="com.example.myproject" />
            </intent-filter>
        </receiver>

public static boolean isForeground = false;

    @Override
    protected void onResume() {
        isForeground = true;
        super.onResume();
    }

    @Override
    protected void onPause() {
        isForeground = false;
        super.onPause();
    }


    private MessageReceiver mMessageReceiver;
    public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION";
    public static final String KEY_TITLE = "title";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EXTRAS = "extras";

    public void registerMessageReceiver() {
        mMessageReceiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(MESSAGE_RECEIVED_ACTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, filter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
                    String messge = intent.getStringExtra(KEY_MESSAGE);
                    String extras = intent.getStringExtra(KEY_EXTRAS);
                    StringBuilder showMsg = new StringBuilder();
                    showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
                    if (!ExampleUtil.isEmpty(extras)) {
                        showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
                    }
                    setCostomMsg(showMsg.toString());
                }
            } catch (Exception e){
            }
        }
    }

 setCostomMsg(showMsg.toString());//删除

Toast.makeText(context, showMsg.toString(), Toast.LENGTH_SHORT).show();

上一篇下一篇

猜你喜欢

热点阅读