Android Notification 手机系统横幅弹出提示框调用,横幅通知,RemoteViews使用实例
生活随笔
收集整理的這篇文章主要介紹了
Android Notification 手机系统横幅弹出提示框调用,横幅通知,RemoteViews使用实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
直接上代碼
:bundle是極光推送的bundle
?
@Override public void onReceive(Context context, Intent intent) {try {Bundle bundle = intent.getExtras();。。。。。。
發送橫幅通知方法:
try {RemoteViews customView = new RemoteViews(context.getPackageName(), R.layout.kongreveiver);NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);String time = getTime(); // String hhmm = timetodate(time);customView.setTextViewText(R.id.timemy, time);if (!TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_TITLE))) {customView.setTextViewText(R.id.name, bundle.getString(JPushInterface.EXTRA_TITLE));}String ones = bundle.getString(JPushInterface.EXTRA_MESSAGE);JSONObject jsonObject = new JSONObject(ones);String title = jsonObject.getString("title");String type = jsonObject.getString("type");String content = jsonObject.getString("content");customView.setTextViewText(R.id.neirongte, title + ":" + content);Intent intentCancel = new Intent(context, NotificationBroadcastReceiver.class);PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(context, 0,intentCancel, PendingIntent.FLAG_ONE_SHOT);Intent companyIntroduce = new Intent(context, TwoMainActivity.class);companyIntroduce.putExtra("type", type); // companyIntroduce.putExtra("name", name);int notifyId = (int) System.currentTimeMillis();PendingIntent pendingIntent = PendingIntent.getActivity(context, notifyId, companyIntroduce, PendingIntent.FLAG_UPDATE_CURRENT);NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);mBuilder//設置通知欄標題 // .setContentText(bundle.getString(JPushInterface.EXTRA_MESSAGE)) //設置通知欄顯示內容.setContent(customView).setContentIntent(pendingIntent) //設置通知欄點擊意圖.setDeleteIntent(pendingIntentCancel)//取消消息回調 // .setTicker(context.getPackageName() + "消息")//通知首次出現在通知欄,帶上升動畫效果的.setWhen(System.currentTimeMillis())//通知產生的時間,會在通知信息里顯示,一般是系統獲取到的時間.setPriority(Notification.PRIORITY_DEFAULT) //設置該通知優先級.setAutoCancel(true)//設置這個標志當用戶單擊面板就可以讓通知將自動取消 //.setOngoing(false)//ture,設置他為一個正在進行的通知。他們通常是用來表示一個后臺任務,用戶積極參與(如播放音樂)或以某種方式正在等待,因此占用設備(如一個文件下載,同步操作,主動網絡連接).setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加聲音、閃燈和振動效果的最簡單、最一致的方式是使用當前的用戶默認設置,使用defaults屬性,可以組合.setDefaults(Notification.DEFAULT_SOUND) //Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加聲音 // requires VIBRATE permission.setSmallIcon(R.mipmap.txlogo); // mNotificationManager.notify(notifyId, notify); // Log.i("lgqq","body=====id============"+tzid+"....."+ones);int num = ShareUtil.getSharedInt("num");num++;ShareUtil.sharedPint("num", num);mNotificationManager.notify(num, mBuilder.build());} catch (JSONException e) {e.printStackTrace();}時間工具:
public String getTime() {long currentTime = System.currentTimeMillis();SimpleDateFormat formatter = new SimpleDateFormat("HH時mm分");Date date = new Date(currentTime);System.out.println(formatter.format(date));// long time=System.currentTimeMillis()/1000;//獲取系統時間的10位的時間戳String str = String.valueOf(formatter.format(date));return str;}?
通知的布局文件內容kongreveiver.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:background="@color/white"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="match_parent"android:gravity="center_vertical"android:orientation="horizontal"android:weightSum="6"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="4.8"android:gravity="center_vertical"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><TextViewandroid:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@color/black_cc"android:layout_marginLeft="12dp"android:text="天鑫運維"/><TextViewandroid:id="@+id/timemy"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="end"android:text="sssss"android:layout_marginRight="40dp"android:textColor="@color/numtext" /></LinearLayout><TextViewandroid:layout_marginLeft="12dp"android:id="@+id/neirongte"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="neir"android:textColor="@color/bartoptec"android:textSize="16dp"android:layout_marginBottom="10dp"android:layout_marginTop="2dp" /></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1.2"android:gravity="center"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="5dp"android:src="@mipmap/ic_launcher"/></LinearLayout></LinearLayout></LinearLayout>?
?系統通知
?
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(APPlocation.getTotalContext()); NotificationManager mNotificationManager = (NotificationManager) APPlocation.getTotalContext().getSystemService(Context.NOTIFICATION_SERVICE);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationChannel channel = new NotificationChannel("com.tianxin.service2thread",TAG,NotificationManager.IMPORTANCE_DEFAULT);mNotificationManager.createNotificationChannel(channel);}mBuilder.setChannelId("com.tianxin.service2thread");//當前包名 mBuilder.setContentText("內容"); Intent notificationIntent = new Intent(this, TwoActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0); mBuilder.setContentIntent(pendingIntent); mBuilder.setSmallIcon(R.mipmap.ic_launcher); mNotificationManager.notify(1, mBuilder.build());?
Android 8.0系統不出通知解決方法:https://blog.csdn.net/meixi_android/article/details/83379335
總結
以上是生活随笔為你收集整理的Android Notification 手机系统横幅弹出提示框调用,横幅通知,RemoteViews使用实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql语句Order by 报错列名不明
- 下一篇: 关于Aws SNS的使用 小结