[英]NotificationCompat.Builder setLargeIcon() not working?
[英]NotificationCompat.Builder action is not working
我正在嘗試在foreground service notification
添加action
。 但是動作點擊從未觸發pending intent
。 我嘗試了以下兩種方法。
Intent stopActionIntent = new Intent(STOP_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyService.this, 0, stopActionIntent,0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.addAction(0,"stop",pendingIntent);
<receiver android:name="com.myproject.receivers.ServiceActionReceiver">
<intent-filter >
<action android:name="com.myproject.services.myservice.STOP_SERVICE"/>
</intent-filter>
</receiver>
@Override
public void onReceive(Context context, Intent intent) {
if(intent != null && intent.getAction().equals(MyService.STOP_SERVICE)){
context.stopService(new Intent(context,MyService.class));
}
}
但是廣播接收者從未打電話。
if(intent != null && STOP_SERVICE.equals(intent.getAction())){
stopSelf();
return super.onStartCommand(intent,flags,startId);
}
else {
Intent stopActionIntent = new Intent(this,MyService.class);
stopActionIntent.setAction(STOP_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(MyService.this, 0, stopActionIntent,0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.addAction(0,"stop",pendingIntent);
}
兩種方法均無效。
defaultConfig {
applicationId "com.myproject.project"
minSdkVersion 16
targetSdkVersion 27
versionCode 2
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
注意:通知和操作可見。
在Android 8.0或更高版本上不起作用,其他版本也可以。
嘗試以下代碼進行通知:
public void heads_up_notification() {
Notification.Builder mBuilder = new Notification.Builder(this);
NotificationManager nNotificationManager = (NotificationManager) getSystemService("notification");
PendingIntent piDismiss = PendingIntent.getActivity(this, 0, new Intent(this, DirectReplyActivity.class), 0);
Intent snoozeIntent = new Intent(this, MainActivity.class);
snoozeIntent.setAction(NotificationCompat.CATEGORY_ALARM);
PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);
mBuilder.setSmallIcon(C0220R.drawable.ic_launcher_background);
mBuilder.setContentTitle("Heads up Notification");
mBuilder.setContentText("heads up activated");
mBuilder.setDefaults(-1);
mBuilder.setPriority(1);
mBuilder.addAction(C0220R.mipmap.ic_dismiss, "Dismiss", piDismiss);
mBuilder.addAction(C0220R.mipmap.ic_stop, "Stop", piSnooze);
nNotificationManager.notify(2, mBuilder.build());
}
在Android O中,必須在Notification Builder中使用頻道。
示例代碼:
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
String CHANNEL_`enter code here`ID = "my_channel_01";// The id of the channel.
CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
// Create a notifi`enter code here`cation and set the notification channel.
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setChannelId(CHANNEL_ID)
.build();
@Shantanu,您有多個問題。 我建議您創建另一個新的示例項目,以使您的概念清晰明了,一旦被清除,請在現有項目中使用它。 您有多個問題,讓我們一一解決-
Manifest.xml:此文件必須具有我們在應用程序中所需的權限。 我在下面發布了一個示例文件,其中我正在處理多個權限,並且在特定事件中我正在致電接收方。 我希望我的接收器保持清醒狀態,並在每次呼出和呼入或未接來電時得到呼叫。
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".WatchMan"
android:enabled="true"
android:exported="true" />
<receiver
android:name=".Receiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity android:name=".developer_activity" />
<activity android:name=".WhiteListActivity" />
<activity android:name=".Contacts"></activity>
</application>
application
標簽上方即可。 而已。 現在,在android studio / eclipse中從->您的項目資源管理器中創建一個服務。右鍵單擊它->選擇new
->選擇service
-> service
,它將為您打開對話框並為您的服務命名。
它將為您創建一個服務類,還將為您修改manifest.xml。 您不需要為此編輯manifest.xml。您可以看一下我上面的清單文件service
標簽。 當我創建這樣的服務時,它會自動為我創建。
現在如何在Android系統中觸發時創建接收特定事件的接收器:
再次轉到項目瀏覽器->右鍵單擊-> new
-> other
-> broadcast receiver
。 它還會為您打開一個對話框,並為您的接收者命名。 同樣,您不需要手動修改清單文件。 這將自動修改您的manifest.xml。 您可以再次參考上面的清單文件。 看看服務,並為我創建接收器。
現在,只要上述新呼叫開始,如何呼叫該接收者即可。
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
在接收器標簽內; 這意味着我的接收器將始終被這兩個事件調用。
現在Receiver.java:
在您的接收器的onReceive函數中
Log.d("RECEIVER ","\SUCCESS : ");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
context.startForegroundService(new Intent(context, WatchMan.class));
}
else
{
context.startService(new Intent(context, WatchMan.class));
}
同樣在這里..您無需在此處手動編寫功能代碼。 您可以通過右鍵單擊擴展類BroadcastReceiver
> generate
- override methods
並選擇onReceive
來創建示例覆蓋函數。 它將為您創建onReceive示例方法。 您必須在其中插入上面的代碼以調用您的服務(前台服務)
現在服務:
轉到服務類。 右鍵單擊擴展類service
-> generate
-> override methods
以及所需的任何方法。 必須有空的服務方法oncreate,onStartCommand,onDestroy和onBind。 同樣,您可以使用與上述相同的創建方法來創建示例標准方法。
現在通知:
服務類聲明:
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
NotificationChannel notificationChannel;
String NOTIFICATION_CHANNEL_ID = "2";
在OnCreate方法中:
try
{
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this, null);
mBuilder.setContentTitle("App name")
.setContentText("Notification text..")
.setTicker("Notification text..")
.setSmallIcon(R.drawable.ic_service_success)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mNotifyManager.createNotificationChannel(notificationChannel);
}
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
startForeground(2, mBuilder.build());
}
catch(Exception e)
{
Log.d("xx", "EXCEPTION IN SHOWING NOTIFICATION...\n");
Log.e("xx", "Exception is : ", e);
}
現在由於調用了啟動前台,它將開始運行您的Onstartcommand
方法
在onstartcommand中,您的邏輯和代碼就在那兒...由您決定是否實現runnable thread
。 它是可選的。
您可以再次顯示其他通知,例如:
mBuilder.setContentText("Some success or failure...");
mBuilder.setTicker("Some success or failure...");
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
startForeground(2, mBuilder.build());
而已。 它應該完成您想要的所有事情。權限,事件,接收者,服務(前台)和通知在4.0到8.0個androids設備上接近99.8%的設備。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.