百度推送(Push)简单集成

何飞翰
2023-12-01


//百度推送初始化配置
private void initPush() {
		
		
		 Utils.logStringCache = Utils.getLogText(getApplicationContext());


	        Resources resource = this.getResources();
	        String pkgName = this.getPackageName();
		
		 PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY,
	                Utils.getMetaValue(MainActivity.this, "api_key"));
	        // Push: 如果想基于地理位置推送,可以打开支持地理位置的推送的开关
	        // PushManager.enableLbs(getApplicationContext());


	        // Push: 设置自定义的通知样式,具体API介绍见用户手册,如果想使用系统默认的可以不加这段代码
	        // 请在通知推送界面中,高级设置->通知栏样式->自定义样式,选中并且填写值:1,
	        // 与下方代码中 PushManager.setNotificationBuilder(this, 1, cBuilder)中的第二个参数对应
	        CustomPushNotificationBuilder cBuilder = new CustomPushNotificationBuilder(
	                resource.getIdentifier(
	                        "notification_custom_builder", "layout", pkgName),
	                resource.getIdentifier("notification_icon", "id", pkgName),
	                resource.getIdentifier("notification_title", "id", pkgName),
	                resource.getIdentifier("notification_text", "id", pkgName));
	        cBuilder.setNotificationFlags(Notification.FLAG_AUTO_CANCEL);
	        cBuilder.setNotificationDefaults(Notification.DEFAULT_VIBRATE);
	        cBuilder.setStatusbarIcon(this.getApplicationInfo().icon);
	        cBuilder.setLayoutDrawable(resource.getIdentifier(
	                "simple_notification_icon", "drawable", pkgName));
	        cBuilder.setNotificationSound(Uri.withAppendedPath(
	                Audio.Media.INTERNAL_CONTENT_URI, "6").toString());
	        // 推送高级设置,通知栏样式设置为下面的ID
	        PushManager.setNotificationBuilder(this, 1, cBuilder);
		
	}
<span style="background-color: rgb(255, 0, 0);"><strong><span style="font-size:18px;">//更改AndroidManifest.xml文件</span></strong></span>
<span style="background-color: rgb(255, 0, 0);"><strong><span style="font-size:18px;"> <!-- 在百度开发者中心查询应用的API Key -->
        <meta-data
            android:name="api_key"
            android:value="Ylg0X8oMflhuMuEWRAKeium8" /></span></strong></span>





 类似资料: