我正在使用云服务提供商(FIREBASE),我在这个过程中得到了股票。 1。在应用上发送消息。将计算numMessages中的通知。 代码就像:
int numMessage:
然后是二传手:
message.setnumMessage(numMessages);
然后是柜台:
numMessages++
现在我的firebase聊天的结构将是输出,
name:test
numMessages:1
readCount:0
现在我想要的是当我点击Pending intent活动时。 readCount将被更改为numMessages的值。 在这种情况下,当我点击通知时,readCount必须更改为1,因为numMessages的值为1.
PendingIntent pIntent =
PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MainActivity.class), 0);
pIntent.equals(readCount=numMessages);
if (numMessages > 0 && targetId == R.id.button) {
Notification noti = new Notification.Builder(MainActivity.this)
.setTicker("You Have " + numMessages + " message")
.setContentTitle("test")
.setContentText("")
.setSmallIcon(R.drawable.ic_od_icon_24dp)
.setContentIntent(pIntent).getNotification();
noti.defaults |= Notification.DEFAULT_SOUND;
noti.defaults |= Notification.DEFAULT_VIBRATE;
noti.ledARGB = Color.BLUE;
noti.ledOnMS = LED_ON_MS;
noti.ledOffMS = LED_OFF_MS;
noti.flags = Notification.FLAG_SHOW_LIGHTS;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID, noti);
}
答案 0 :(得分:1)
修改强> 您希望每次接收通知,更改numMessages的值,以及应用程序中的某个位置,将numMessages的值更新为readCount? 如果我理解您的问题,可以尝试以下步骤:
一个。创建接收器以在接收通知时进行处理
B中。从通知中获取变量的值:
并且,将值设置为通知:
类似于从通知中获取价值。使用静态变量或数据库或SharedPreference
一些示例代码:
MainActivity.class:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onGenNoti(View v) {
// Check readCount
Variables.readCount = Variables.numMessages;
PendingIntent pIntent =
PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MainActivity.class), 0);
if (Variables.numMessages > 0) {
Notification noti = new Notification.Builder(MainActivity.this)
.setTicker("You Have " + Variables.numMessages + " message")
.setContentTitle("test")
.setContentText("")
.setSmallIcon(R.drawable.ic_od_icon_24dp)
.setContentIntent(pIntent).getNotification();
noti.defaults |= Notification.DEFAULT_SOUND;
noti.defaults |= Notification.DEFAULT_VIBRATE;
noti.ledARGB = Color.BLUE;
noti.ledOnMS = LED_ON_MS;
noti.ledOffMS = LED_OFF_MS;
noti.flags = Notification.FLAG_SHOW_LIGHTS;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID, noti);
}
}
}
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate Noti"
android:onClick="onGenNoti"/>
</LinearLayout>
Variables.class
public class Variables {
public static int numMessages;
public static int readCount;
}
Receiver.class:
public class Receiver extends BroadcastReceiver {
private static final Object ACTION = "MyNotification";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION.equals(action)) {
//do what you want here
Variables.numMessages++;
generateNotification(context,"MyNotification");
}
}
private void generateNotification(Context context, String message) {
long when = System.currentTimeMillis();
int icon = R.drawable.ic_launcher;
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
// String subTitle = context.getString(R.string.app_name);
String subTitle = "some text";
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("content", message);
PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(context, title, subTitle, intent);
//To play the default sound with your notification:
//notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
最后,在Manifest中的应用程序标记中添加这些行
<receiver
android:name="your.package.Receiver" >
<intent-filter>
<action android:name="MyNotification" />
</intent-filter>
</receiver>