我正在使用云服务提供商(FIREBASE),在此过程中我有货。
1号在应用上发送消息。将在numMessages中对通知进行计数。
代码是这样的:
int numMessage:
然后是二传手:
message.setnumMessage(numMessages);
然后柜台:
numMessages++
现在在我的firebase聊天的结构上,这将是输出,
name:test
numMessages:1
readCount:0
现在我想要的是当我单击Pending intent activity时。 readCount将更改为numMessages的值。
在这种情况下,当我单击通知时,由于numMessages的值为1,因此readCount必须更改为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);
}
最佳答案
编辑:
您希望每次都收到通知,它会更改numMessages的值,并在您的应用程序中的某个位置将numMessages的值更新为readCount吗?
如果我了解您的问题,请尝试以下步骤:
A.创建接收方以在收到通知时进行处理
B.从Notification获取变量的值:
您可以将静态变量用于numMessages,并在其他readCount上对其进行引用。
或者,使用数据库或SharedPreference
并且,将值设置为Notification:
类似于从通知中获取价值。使用静态变量或数据库或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);
}
}
最后,在清单中的应用程序标记内添加这些行
<receiver
android:name="your.package.Receiver" >
<intent-filter>
<action android:name="MyNotification" />
</intent-filter>
</receiver>