显示最后一个通知推送,我需要显示所有通知推送

时间:2015-04-13 20:10:37

标签: ibm-mobilefirst mobilefirst-studio

我正在使用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 Native Push Notifications Tags Based。

我找到了答案

[其他答案相同] [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;
}

}

和nex是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>

并在下一条消息中显示错误:

“您的清单不允许使用push.Android Manifest错误:清单中缺少意图服务:com.worklight.wlclient.push.GCMIntentService

1 个答案:

答案 0 :(得分:0)

Worklight / MFP无法内置此功能。

也就是说,您可以覆盖默认实现并自行完成。
查看Yoel Nunez撰写的以下博客文章,其中介绍了如何进行此类覆盖并实现类似功能:InboxStyle Notifications in Android