Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Notification not appearing using React Native java NotificationBuilder

$
0
0

I am using React Native not expo go version.I am calling from React Native Js files the Native Module to start the foreground service , I have observed that the foreground service starts correctly and works as it should , however the notification which shows that this is running in background doesn't appear

The App.jsx

import { NativeModules } from 'react-native';const Clip = NativeModules.ClipboardService;export const App = () => {  const [whatsNextYCoord, setWhatsNextYCoord] = useState(0);  const scrollViewRef = useRef(null);  useEffect(() => {    if (Clip) {      console.log({ asd: Clip });      Clip.startService();    } else {      console.log('ClipboardTest module is not available');    }  }, []);

The Function that starts the service

    @ReactMethod    public void startService() {        Log.d("ClipboardService", "Starting Clipboard Listener Service");        Intent serviceIntent = new Intent(getReactApplicationContext(), ClipboardListenerService.class);        // Use ContextCompat.startForegroundService() to start your service        ContextCompat.startForegroundService(getReactApplicationContext(), serviceIntent);    }

And the actual Service ( I didnt include the un-necessary part of the clipboard listener)

public class ClipboardListenerService extends Service {    private ClipboardManager clipboardManager;    private static final String CHANNEL_ID = "clipboard_service_channel";    @Override    public void onCreate() {        super.onCreate();        Log.d("ClipboardService", "Service Created");        createNotificationChannel();        startForegroundService();        clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);        clipboardManager.addPrimaryClipChangedListener(this::onPrimaryClipChanged);    } private void startForegroundService() {        Log.d("ClipboardService", "Starting Foreground Service");        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)                .setContentTitle("Clipboard Service")                .setContentText("Listening to clipboard changes")                .setSmallIcon(R.drawable.abacus) // Make sure this is a valid drawable resource                .setPriority(NotificationCompat.PRIORITY_HIGH); // Set a high priority to make it more likely to be shown        startForeground(1, builder.build());    }    private void createNotificationChannel() {        Log.d("ClipboardService", "createNotificationChannel");        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            CharSequence name = getString(R.string.channel_name);            String description = getString(R.string.channel_description);            int importance = NotificationManager.IMPORTANCE_HIGH;            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);            channel.setDescription(description);            NotificationManager notificationManager = getSystemService(NotificationManager.class);            notificationManager.createNotificationChannel(channel);        }    }

I have also included the requirements needed in the AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.FOREGROUND_SERVICE" /><uses-permission android:name="android.permission.POST_NOTIFICATIONS"/><application      android:name=".MainApplication"      android:label="@string/app_name"      android:icon="@mipmap/ic_launcher"      android:roundIcon="@mipmap/ic_launcher_round"      android:allowBackup="false"      android:theme="@style/AppTheme"      android:networkSecurityConfig="@xml/network_security_config"><activity        android:name=".MainActivity"        android:label="@string/app_name"        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"        android:launchMode="singleTask"        android:windowSoftInputMode="adjustResize"        android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><service        android:name=".clipboardService.ClipboardListenerService"        android:enabled="true"        android:exported="false"        android:foregroundServiceType="dataSync" /></application></manifest>

My suspicion that is maybe wrong since I'm not an Android dev is that the issue comes from the Intent class which gets used to make an intent for the service.

No matter what i have tried the notification does not appear at all in the notification tray.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>