【安卓】功能型消息推送功能如何实现(带同意 拒绝)

【安卓】功能型消息推送功能如何实现(带同意 拒绝)

比如如上图的那种推送功能
现在哪些推送平台可以实现这种功能

回答

@erehmi 说的不错,根据厂商会有所不同。

给你举个原生android例子:

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

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center"

android:orientation="horizontal" >

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:text="DJ notification"

android:textAppearance="?android:attr/textAppearanceMedium" />

<Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Close Me" />

</LinearLayout>

RemoteViews remoteViews = new RemoteViews(getPackageName(),  

R.layout.widget);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(

this).setSmallIcon(R.drawable.ic_launcher).setContent(

remoteViews);

// Creates an explicit intent for an Activity in your app

Intent resultIntent = new Intent(this, test.class);

// The stack builder object will contain an artificial back stack for

// the

// started Activity.

// This ensures that navigating backward from the Activity leads out of

// your application to the Home screen.

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself)

stackBuilder.addParentStack(test.class);

// Adds the Intent that starts the Activity to the top of the stack

stackBuilder.addNextIntent(resultIntent);

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,

PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.button1, resultPendingIntent);

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// mId allows you to update the notification later on.

mNotificationManager.notify(100, mBuilder.build());

推送一般分为两种:

  1. 推送通知

  2. 透传消息

推送通知一般是推送提供商帮你已经实现基本界面交互(包括收到通知后状态栏通知消息提醒, 通知点击行为), 这类是比较常用的. 而透传消息它只负责接收, 而不管前端展示, 这种消息正好能解决题主的需求: 客户端收到透传消息后, 自己编写代码弹出自定义样式的状态栏通知.

请参考各大推送厂商的SDK文档.

iOS在8.0之后就可以自定义推送信息显示按钮, 并且通过代理监听用户对推送消息操作.

1.在UIApplication中有对推送消息进行设置的方法

- (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings

【安卓】功能型消息推送功能如何实现(带同意 拒绝)
2.UIUserNotificationSettings类中可以对消息添加用户行为(如: 按钮)

+ (instancetype)settingsForTypes:(UIUserNotificationType)types

categories:(nullable NSSet<UIUserNotificationCategory *> *)categories;

【安卓】功能型消息推送功能如何实现(带同意 拒绝)

3.添加UIUserNotificationAction,用于用户操作.

- (void)setActions:(nullable NSArray<UIUserNotificationAction *> *)actions forContext:(UIUserNotificationActionContext)context;

【安卓】功能型消息推送功能如何实现(带同意 拒绝)

推送截图:

【安卓】功能型消息推送功能如何实现(带同意 拒绝)

代码:(参考链接)

[[UIApplication sharedApplication] registerForRemoteNotifications];

UIMutableUserNotificationAction *actionAccept = [[UIMutableUserNotificationAction alloc] init];

actionAccept.identifier = @"accept";

[email protected]"√ 允许";

actionAccept.activationMode = UIUserNotificationActivationModeForeground;

actionAccept.destructive = YES;

UIMutableUserNotificationAction * actionReject = [[UIMutableUserNotificationAction alloc] init];

actionReject.identifier = @"reject";

[email protected]"× 拒绝";

actionReject.activationMode = UIUserNotificationActivationModeBackground;

actionReject.authenticationRequired = NO;

actionReject.destructive = NO;

UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];

category.identifier = @"Category";

[category setActions:@[actionReject,actionAccept] forContext:(UIUserNotificationActionContextDefault)];

UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects: category, nil]];

[[UIApplication sharedApplication] registerUserNotificationSettings: uns];

使用自定义消息都可以做到。

题主说的这个是通知的样式,和推送关系不大。虽然推送通常也会带一个简单样式的默认通知,但是一般还是会使用自定义消息然后自己去定制通知的样式。

以上是 【安卓】功能型消息推送功能如何实现(带同意 拒绝) 的全部内容, 来源链接: utcz.com/a/97775.html

回到顶部