Android 极光推送API

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import cn.jpush.android.api.JPushInterface;

import cn.jpush.android.helper.Logger;

public class ExampleUtil {

public static final String PREFS_NAME = “JPUSH_EXAMPLE”;

public static final String PREFS_DAYS = “JPUSH_EXAMPLE_DAYS”;

public static final String PREFS_START_TIME = “PREFS_START_TIME”;

public static final String PREFS_END_TIME = “PREFS_END_TIME”;

public static final String KEY_APP_KEY = “JPUSH_APPKEY”;

public static boolean isEmpty(String s) {

if (null == s)

return true;

if (s.length() == 0)

return true;

if (s.trim().length() == 0)

return true;

return false;

}

/**

  • 只能以 “+” 或者 数字开头;后面的内容只能包含 “-” 和 数字。

  • */

private final static String MOBILE_NUMBER_CHARS = “1[-0-9]{1,}$”;

public static boolean isValidMobileNumber(String s) {

if(TextUtils.isEmpty(s)) return true;

Pattern p = Pattern.compile(MOBILE_NUMBER_CHARS);

Matcher m = p.matcher(s);

return m.matches();

}

// 校验Tag Alias 只能是数字,英文字母和中文

public static boolean isValidTagAndAlias(String s) {

Pattern p = Pattern.compile(“^[\u4E00-\u9FA50-9a-zA-Z_!@#KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲*+=.|]+”);

Matcher m = p.matcher(s);

return m.matches();

}

// 取得AppKey

public static String getAppKey(Context context) {

Bundle metaData = null;

String appKey = null;

try {

ApplicationInfo ai = context.getPackageManager().getApplicationInfo(

context.getPackageName(), PackageManager.GET_META_DATA);

if (null != ai)

metaData = ai.metaData;

if (null != metaData) {

appKey = metaData.getString(KEY_APP_KEY);

if ((null == appKey) || appKey.length() != 24) {

appKey = null;

}

}

} catch (NameNotFoundException e) {

}

return appKey;

}

// 取得版本号

public static String GetVersion(Context context) {

try {

PackageInfo manager = context.getPackageManager().getPackageInfo(

context.getPackageName(), 0);

return manager.versionName;

} catch (NameNotFoundException e) {

return “Unknown”;

}

}

public static void showToast(final String toast, final Context context)

{

new Thread(new Runnable() {

@Override

public void run() {

Looper.prepare();

Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();

Looper.loop();

}

}).start();

}

public static boolean isConnected(Context context) {

ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo info = conn.getActiveNetworkInfo();

return (info != null && info.isConnected());

}

public static String getImei(Context context, String imei) {

String ret = null;

try {

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

ret = telephonyManager.getDeviceId();

} catch (Exception e) {

Logger.e(ExampleUtil.class.getSimpleName(), e.getMessage());

}

if (isReadableASCII(ret)){

return ret;

} else {

return imei;

}

}

private static boolean isReadableASCII(CharSequence string){

if (TextUtils.isEmpty(string)) return false;

try {

Pattern p = Pattern.compile(“[\x20-\x7E]+”);

return p.matcher(string).matches();

} catch (Throwable e){

return true;

}

}

public static String getDeviceId(Context context) {

return JPushInterface.getUdid(context);

}

}

在这里插入图片描述

⑤ 修改MainActivity.java

然后是MainActiviity.java

//for receive customer msg from jpush server

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”;

private EditText msgText;

注册消息接收和设置自定义消息

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){

}

}

}

//设置自定义消息

private void setCostomMsg(String msg){

if (null != msgText) {

msgText.setText(msg);

msgText.setVisibility(View.VISIBLE);

}

}

然后在onCreate中初始化和调用

JPushInterface.init(getApplicationContext());//极光接口初始化,否则用不了

registerMessageReceiver();//注册消息接收器

现在你就可以运行了

在这里插入图片描述

⑥ 发送通知

很好,看到Hello World!了,现在打开极光的控制台

在这里插入图片描述

点击进入

在这里插入图片描述

因为我已经安装了应用,所以在平台上可以看到新增了一个用户。

设置推送消息

在这里插入图片描述

滑动到最下面,广播所有人的意思就是只要是安装了这个应用的人都会收到通知

在这里插入图片描述

然后预览

在这里插入图片描述

然后你会看到预估人数1,就算你这里是0也没有关系,因为这个平台的数据有时候会有延时,不用担心,大胆的勇敢的点击确认发送通知吧!

在这里插入图片描述

发送成功!

而且手机上也收到了通知了

在这里插入图片描述

[](ht外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

资料获取→专栏
tps://llw-study.blog.csdn.net/article/details/106180194)⑦ 点击通知跳转页面

在使用其他的APP的时候点击通知的时候通常会打开不同的页面或者不同的URL,而目前你要是点击这个通知的话就是重新打开当前应用,这显然不是那么的合理,所以当我们需要点击通知跳转到不同页面时,要怎么做呢?

这个方面的功能极光中并没有详细说明,我也是经过反复测试和摸索才总结出来的,回到PushReceiver,在这里之前只做了一个简单的继承,而且是也是在这里做通知点击之后的业务处理的。

因此我需要重写onNotifyMessageOpened方法。它是一个通知栏点击的监听,我只要在点击的时候跳转到其他页面就行了,非常的简单吧。

不过呢?首先需要新建一个页面才行,就取名TestActivity。

在这里插入图片描述

然后进入到PushReceiver

package com.llw.pushdemo.receiver;

import android.content.Context;

import android.content.Intent;

import com.llw.pushdemo.TestActivity;

import cn.jpush.android.api.NotificationMessage;

import cn.jpush.android.service.JPushMessageReceiver;

public class PushReceiver extends JPushMessageReceiver {

@Override

public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {

Intent intent = new Intent(context, TestActivity.class);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );

context.startActivity(intent);

}

}

很简单的代码对不对,就是跳转页面而已。

下面直接运行吧,通过极光平台发送通知,App收到通知,然后点击通知栏。

在这里插入图片描述

这样它就可以跳转到TestActivity页面了。而如果你要携带一些参数呢?

也很简单,

在这里插入图片描述

在极光平台上发送通知的时候,配置附加字段。然后回到PushReceiver

在这里插入图片描述

然后修改一些activity_test.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:gravity=“center”

android:orientation=“vertical”

tools:context=“.TestActivity”>

<TextView

android:id=“@+id/tv_test”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textColor=“#000”

android:textSize=“20sp” />

然后在TestActivity中

package com.llw.pushdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.widget.TextView;

public class TestActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setConte
ntView(R.layout.activity_test);

TextView tvTest = findViewById(R.id.tv_test);

String extras = getIntent().getStringExtra(“extras”);

if(extras !=null){

tvTest.setText(extras);

}

}

}

代码也很简单,通过getIntent拿到传递过来的数据,然后显示在TextView上,下面来试试吧。

在这里插入图片描述

OK,内容就有了。

⑧ 拓展 应用通知开关监听

你看,就这样实现了。你以为就完了吗?

在这里插入图片描述

当然没有完!注意到上面的图是推送消息的记录,目标1,成功1,当然有的手机会收不到通知,这是为什么呢?因为国内的很多手机厂商对Android系统进行了自家系统开发,导致,Android的兼容比较难做,因为有的手机默认应用就不允许接收通知,所以你收不到也不要觉得奇怪,在手机设置里打开通知开关就可以了。

我的是荣耀 20i,默认安装应用就自动打开了这个开关的。

这里就涉及到另一个知识点了,那就是通知开关的监听。举个例子,爱奇艺APP,平时老是给我推送通知,烦得很,然后我就给它关了通知,下次进入APP是会有一个提示

在这里插入图片描述

就像这样,基本每个成熟的APP,都会有这个监听的。下面来看看怎么做吧。

无非就是两个方法而已

//是否开启通知接收

private boolean isNotificationEnabled(Context context) {

boolean isOpened = false;

try {

isOpened = NotificationManagerCompat.from(context).areNotificationsEnabled();

} catch (Exception e) {

e.printStackTrace();

isOpened = false;

}

return isOpened;

}

//去设置

private void gotoSet() {

Intent intent = new Intent();

if (Build.VERSION.SDK_INT >= 26) {

// android 8.0引导

intent.setAction(“android.settings.APP_NOTIFICATION_SETTINGS”);

intent.putExtra(“android.provider.extra.APP_PACKAGE”, getPackageName());

} else if (Build.VERSION.SDK_INT >= 21) {

// android 5.0-7.0

intent.setAction(“android.settings.APP_NOTIFICATION_SETTINGS”);

intent.putExtra(“app_package”, getPackageName());

intent.putExtra(“app_uid”, getApplicationInfo().uid);

} else {

// 其他

intent.setAction(“android.settings.APPLICATION_DETAILS_SETTINGS”);

intent.setData(Uri.fromParts(“package”, getPackageName(), null));

}

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

}

然后就是使用方法了

在这里插入图片描述

这个时候如果你的这个Demo通知是关闭的话,那么你一打开这个页面就会跳转到通知开启那里去。OK,你以为完了吗?

真的完了!

最后贴一下MainActiviy.java的完整代码

package com.llw.pushdemo;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.NotificationManagerCompat;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.net.Uri;

import android.os.Build;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import cn.jpush.android.api.JPushInterface;

public class MainActivity extends AppCompatActivity {

//for receive customer msg from jpush server

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”;

private EditText msgText;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

JPushInterface.init(getApplicationContext());//极光接口初始化,否则用不了

registerMessageReceiver();//注册消息接收器

//判断该app是否打开了通知,如果没有的话就打开手机设置页面

if (!isNotificationEnabled(this)) {

//开启通知弹窗

gotoSet();

} else {

//当前app允许消息通知

}

}

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){

}

}

}

//设置自定义消息

private void setCostomMsg(String msg){

if (null != msgText) {

msgText.setText(msg);

msgText.setVisibility(View.VISIBLE);

}

}

//是否开启通知接收

private boolean isNotificationEnabled(Context context) {

boolean isOpened = false;

try {

isOpened = NotificationManagerCompat.from(context).areNotificationsEnabled();

} catch (Exception e) {

e.printStackTrace();

isOpened = false;

}

return isOpened;

}

//去设置

private void gotoSet() {

Intent intent = new Intent();

if (Build.VERSION.SDK_INT >= 26) {

// android 8.0引导

intent.setAction(“android.settings.APP_NOTIFICATION_SETTINGS”);

intent.putExtra(“android.provider.extra.APP_PACKAGE”, getPackageName());

} else if (Build.VERSION.SDK_INT >= 21) {

// android 5.0-7.0

intent.setAction(“android.settings.APP_NOTIFICATION_SETTINGS”);

intent.putExtra(“app_package”, getPackageName());

intent.putExtra(“app_uid”, getApplicationInfo().uid);

} else {

// 其他

intent.setAction(“android.settings.APPLICATION_DETAILS_SETTINGS”);

intent.setData(Uri.fromParts(“package”, getPackageName(), null));

}

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

}

}

= false;

try {

isOpened = NotificationManagerCompat.from(context).areNotificationsEnabled();

} catch (Exception e) {

e.printStackTrace();

isOpened = false;

}

return isOpened;

}

//去设置

private void gotoSet() {

Intent intent = new Intent();

if (Build.VERSION.SDK_INT >= 26) {

// android 8.0引导

intent.setAction(“android.settings.APP_NOTIFICATION_SETTINGS”);

intent.putExtra(“android.provider.extra.APP_PACKAGE”, getPackageName());

} else if (Build.VERSION.SDK_INT >= 21) {

// android 5.0-7.0

intent.setAction(“android.settings.APP_NOTIFICATION_SETTINGS”);

intent.putExtra(“app_package”, getPackageName());

intent.putExtra(“app_uid”, getApplicationInfo().uid);

} else {

// 其他

intent.setAction(“android.settings.APPLICATION_DETAILS_SETTINGS”);

intent.setData(Uri.fromParts(“package”, getPackageName(), null));

}

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

}

}


  1. +0-9 ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值