极光推送

文档

https://docs.jiguang.cn//jpush/advanced/rich_push/

说明 1:若没有 res/drawable-xxxx/jpush_notification_icon 这个资源默认使用应用图标作为通知 icon,在 5.0 以上系统将应用图标作为 statusbar icon 可能显示不正常,用户可定义没有阴影和渐变色的 icon 替换这个文件,文件名不要变。
说明 2:使用 android studio 的开发者,如果使用 jniLibs 文件夹导入 so 文件,则仅需将所有 cpu 类型的文件夹拷进去;如果将 so 文件添加在 module的libs 文件夹下,注意在 module 的 gradle 配置中添加一下配置:

   android {
        ......
        sourceSets {
            main {
                jniLibs.srcDirs = ['libs']
                ......
            }
            ......
        }
        ......
    }

根据 SDK 压缩包里的 AndroidManifest.xml 样例文件,来配置应用程序项目的 AndroidManifest.xml 。

主要步骤为:

复制备注为 "Required" 的部分
将标注为“您应用的包名”的部分,替换为当前应用程序的包名
将标注为“您应用的 Appkey” 的部分,替换为在 Portal 上创建该应用后应用信息中的 Appkey,例如:9fed5bcb7b9b87413678c407

defaultConfig {
      applicationId "cn.jpush.example" // <--您应用的包名
      ……
 }

在 AndroidManifest 中使用 ${applicationId} 引用 gradle 中定义的包名

AndroidManifest 示例



    

    
    

    
    
    
    
    
    
    
    
    
    
    

    
     
    
    
    
    
    
    
    


    

        
        
        
            
                
                
                
                
            
        


    
        

        
        
         
             
                 
                 
             
         

         
          

        
        
          
                
                
            
            
                
                
            
            
            
                
                
                
            
        

        
        
            
                
                
                
            
        
        
        
            
                
                
            
        

        
        
        

        
        

        
        
        
        
            
                
                
            
        

        
         
             
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 
             
         

        
        
         
            
                
                
            
          

        
        
        
        
        
        
        
        
    

代码

public class BaseApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }
}
public class CustomReceiver extends BroadcastReceiver {

    private static final String TAG = "MyReceiver";
    private NotificationManager nm;


    @Override
    public void onReceive(Context context, Intent intent) {
        //用户自定义的广播接收器
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        Bundle bundle = intent.getExtras();
        Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Log.d(TAG, "JPush 用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "接受到推送下来的自定义消息");
            sendNotification(context,bundle.getString(JPushInterface.EXTRA_MESSAGE));
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "接受到推送下来的通知");
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "用户点击打开了通知");

        } else {
            Log.d(TAG, "Unhandled intent - " + intent.getAction());
        }
    }


    private void sendNotification(Context context, String msg) {

        //data是1,跳转Activity展示美女
        //如果是2,点击跳转到主页
        String data = "";
        String message = "";
        String url = "";
        try {
            JSONObject extrasJson = new JSONObject(msg);
            data = extrasJson.optString("data");
            message = extrasJson.optString("msg");
            url = extrasJson.optString("url");
        } catch (Exception e) {
            Log.w(TAG, "Unexpected: extras is not a valid json", e);
            return;
        }

        //发送自己的通知
        String channelId = "me";
        CharSequence channelName = "msg";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
            nm.createNotificationChannel(channel);
        }

        Intent intent;
        //data是1,跳转Activity展示美女
        //如果是2,点击跳转到主页
        if ("1".equals(data)) {
            intent = new Intent(context, MeiNvActivity.class);
        } else{
            intent = new Intent(context, MainActivity.class);
        }
        intent.putExtra("url",url);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //延时意图
        PendingIntent pendingIntent = PendingIntent.getActivity(context,100,
                intent,PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new NotificationCompat.Builder(context, channelId)
                .setSmallIcon(R.mipmap.ic_launcher)//小图标,必要
                .setContentText(message)//消息内容,必要
                .setContentTitle(message)//通知标题,必要
                .setContentIntent(pendingIntent)//延时意图
                .setAutoCancel(true)//点击消失,和延时意图配合使用
                .build();
        nm.notify(1,notification);
    }

    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
                    Log.i(TAG, "This message has no Extra data");
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator it = json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next().toString();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " + json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {
                    Log.e(TAG, "Get message extra JSON error!");
                }

            } else {
                sb.append("key:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }
}

···
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initPer();

}

private void initPer() {
    String[] strings = {Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
    };
    ActivityCompat.requestPermissions(this,strings,100);
}

}

···

···
public class MeiNvActivity extends AppCompatActivity {

private ImageView mIv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mei_nv);
    initView();
    String url = getIntent().getStringExtra("url");
    RequestOptions options = new RequestOptions()
            .placeholder(R.mipmap.ic_launcher).circleCrop();

    Glide.with(this).load(url).apply(options).into(mIv);
}

private void initView() {
    mIv = (ImageView) findViewById(R.id.iv);
}

}

···
5·.

public class MyReceiver extends JPushMessageReceiver {
}


public class WakeReceiver extends WakedResultReceiver {

    @Override
    public void onWake(int i) {
        super.onWake(i);
    }
}

你可能感兴趣的:(极光推送)