0

我正在使用 MobileFirst Studio 平台发送通知推送,在应用程序中我收到通知,但如果向我的应用程序发送多个通知,这只是最后一次通知发送的示例。

我需要在设备通知栏的列表中显示所有通知,每个我收到通知。

这是用于发送通知的适配器的功能代码:

var applicationId="ElUniversalAppAndroid";

function sendNotification(notificationText, tag,url){
var notification = {};

notification.message = {};
notification.message.alert = notificationText;

var tags = tag.split(",");

notification.target = {};
notification.target.tagNames = tags;

// set notification properties for GCM
notification.settings = {};
notification.settings.gcm = {};
notification.settings.gcm.payload = {"url":url};
notification.settings.gcm.sound = 'sinsajo.mp3';

var delay=WL.Server.sendMessage(applicationId, notification);
WL.Logger.error("Notificacion enviada exitosamente " + JSON.stringify(notification));
return { result: "Notification sent"}; 
}

我遵循 IBM Knowledges 中的示例并支持 IBM。

我正在使用 MobileFirst Platform Studio 7.0 和基于 Android 原生推送通知标签。

我找到了答案

[其他答案相同][1]

但我不在 MobileFirst Platform Studio 中工作

接下来是 GCMIntentService 类的代码:

package com.test.eluniversal.tagsuniversal;
import java.util.Iterator;
import java.util.Queue;
import org.json.JSONObject;
import com.worklight.nativeandroid.common.WLUtils;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;

public class GCMIntentService extends
  com.worklight.wlclient.push.GCMIntentService {

  private NotificationObject getNotification(Intent intent) {
    NotificationObject notificationObject=new NotificationObject();
    com.worklight.wlclient.push.GCMIntentService.Message message=
    intent.getParcelableExtra("message");
    notificationObject.setId((int)System.currentTimeMillis());
    String title= "TagsUniversal";
    JSONObject alertJsonObject=message.getProps();
    String alert=alertJsonObject.optString("alert",null);
    notificationObject.setTitle(title);
    notificationObject.setAlert(alert);
    JSONObject payload = message.getPayload();
    String url = payload.optString("url", null);
    notificationObject.setUrl(url);
    return notificationObject;
 }

 @Override
 public void notify(Context context, String alert, int badge, String
  sound,Intent intent) {
   createNotification(context, alert, getNotificationTitle(context),
    alert, badge, sound, intent);
 }

 @Override
 public void notify(Context context, String tickerText) {
   createNotification(context, tickerText,
   getNotificationTitle(context),tickerText, 0, "1", null);
 }

 private void createNotification(Context context, String ticker,
                    String title, String msg, int badge,String sound,
                    Intent intent) {
   int icon = -1;
   long when = System.currentTimeMillis();

   try {
     icon = WLUtils.getResourceId(getApplicationContext(), "drawable",
      "push");
   } catch (Exception e) {
     // ERROR: add icon resource
   }
   NotificationObject notificationObject=getNotification(intent);
   NotificationCompat.Builder mBuilder = new 
   NotificationCompat.Builder(
this).setSmallIcon(icon).setContentTitle(notificationObject.getTitle())
    .setContentText(msg);

mBuilder.setAutoCancel(true);
mBuilder.setTicker(ticker);
mBuilder.setNumber(badge);
mBuilder.setWhen(when);

Intent viewIntent = new Intent(this, ActivityPrincipal.class);
viewIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent viewPendingIntent = PendingIntent.getActivity(this, 0,
    viewIntent, 0);

mBuilder.setContentIntent(viewPendingIntent);

Notification myNotification = mBuilder.build();

NotificationManagerCompat nm = NotificationManagerCompat.from(this);
nm.notify(notificationObject.getId(), myNotification);

// play notification sound, vibrate, etc...

}
}

并使用下一个类模型:

package com.test.eluniversal.tagsuniversal;

public class NotificationObject {
  private int id=0;
  private String title=null;
  private String alert=null;
  private String sound=null;
  private String url=null;
  private String icon=null;

public NotificationObject(){

}

public NotificationObject(int id,String title, String alert, String url){
  this.id=id;
  this.title=title;
  this.alert=alert;
  this.url=url;
}

public int getId() {
  return id;
}

public void setId( int id ) {
  this.id = id;
}

public String getTitle() {
  return title;
}

public void setTitle( String title ) {
  this.title = title;
}

public String getAlert() {
  return alert;
}

public void setAlert( String alert ) {
  this.alert = alert;
}

public String getSound() {
  return sound;
}

public void setSound( String sound ) {
  this.sound = sound;
}

public String getUrl() {
  return url;
}

public void setUrl( String url ) {
  this.url = url;
}

public String getIcon() {
  return icon;
}

public void setIcon( String icon ) {
  this.icon = icon;
}

}

下一个是 Manifest.xml:

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

<permission android:name="com.test.eluniversal.tagsuniversal.permission.C2D_MESSAGE" android:protectionLevel="signature"/>

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>

<uses-permission android:name="com.test.eluniversal.tagsuniversal.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:launchMode="singleTask">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <activity
        android:name=".ActivityPrincipal"
        android:label="@string/app_name"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.test.eluniversal.tagsuniversal.tagsUniversal.NOTIFICATION" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <service android:name="com.test.eluniversal.tagsuniversal.GCMIntentService" />
    <receiver android:name="com.worklight.wlclient.push.WLBroadcastReceiver"
              android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
            <category android:name="com.test.eluniversal.tagsuniversal"/>
        </intent-filter>
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.test.eluniversal.tagsuniversal" />
        </intent-filter>
    </receiver>
</application>

下一条消息错误:

“您的清单不允许与推送一起使用。Android 清单错误:清单中缺少意图服务:com.worklight.wlclient.push.GCMIntentService

4

1 回答 1

0

Worklight/MFP 不提供此内置功能。

也就是说,您可以覆盖默认实现并自行执行此操作。
查看 Yoel Nunez 的以下博客文章,其中解释了如何进行此类覆盖并实现类似的功能:Android 中的 InboxStyle 通知

于 2015-04-13T20:14:55.163 回答