Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

feat(Android): Adding full screen intent support #2112

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ PushNotification.localNotification({

actions: ["Yes", "No"], // (Android only) See the doc for notification actions to know more
invokeApp: true, // (optional) This enable click on actions to bring back the application to foreground or stay in background, default: true
fullScreenIntent: false, // (optional) Specifies to whether or not to launch an intent instead of posting the notification to the status bar.


/* iOS only properties */
category: "", // (optional) default: empty string
Expand Down Expand Up @@ -813,3 +815,41 @@ PushNotification.checkPermissions(callback: Function) //Check permissions
PushNotification.getApplicationIconBadgeNumber(callback: Function) //Get badge number
```

## Full screen intents

(Android Only)

In order to launch an intent instead of posting the notification to the status bar you will have to specify `fullScreenIntent: true` while configuring the local notification.

**NOTE: Apps targeting Build.VERSION_CODES#Q and above will have to request a permission in order to use full screen intents.**
```xml
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
````

**NOTE: To be able to view the intent launched by the app when the device is locked you will have to add the following properties to the MainActivity as follows**
```xml
<activity
android:name=".MainActivity"
...
android:showOnLockScreen="true"
android:showWhenLocked="true"
android:turnScreenOn="true"
...>
</activity>
````

Additionally you will have to add the following code to the `MainActivity.java` file.
```java
@Override
protected void onStart() {
super.onStart();
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
}
...
}

Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
notification.setChannelId(channel_id);
notification.setContentIntent(pendingIntent);

if (bundle.getBoolean("fullScreenIntent"))
notification.setFullScreenIntent(pendingIntent, true);

JSONArray actionsArray = null;
try {
actionsArray = bundle.getString("actions") != null ? new JSONArray(bundle.getString("actions")) : null;
Expand Down
13 changes: 10 additions & 3 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
View,
TouchableOpacity,
Alert,
ScrollView
} from 'react-native';
import NotifService from './NotifService';

Expand All @@ -30,7 +31,7 @@ export default class App extends Component {

render() {
return (
<View style={styles.container}>
<ScrollView contentContainerStyle={styles.container}>
<Text style={styles.title}>
Example app react-native-push-notification
</Text>
Expand Down Expand Up @@ -133,13 +134,20 @@ export default class App extends Component {
}}>
<Text>popInitialNotification</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => {
this.notif.scheduleNotif(null, true);
}}>
<Text>Full screen intent notification in 30s</Text>
</TouchableOpacity>

<View style={styles.spacer}></View>

{this.state.fcmRegistered && <Text>FCM Configured !</Text>}

<View style={styles.spacer}></View>
</View>
</ScrollView>
);
}

Expand All @@ -158,7 +166,6 @@ export default class App extends Component {

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
Expand Down
9 changes: 5 additions & 4 deletions example/NotifService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class NotifService {
PushNotification.setApplicationIconBadgeNumber(0);
}
});

PushNotification.getChannels(function(channels) {
console.log(channels);
});
Expand Down Expand Up @@ -107,7 +107,7 @@ export default class NotifService {
});
}

scheduleNotif(soundName) {
scheduleNotif(soundName, fullScreenIntent) {
this.lastId++;
PushNotification.localNotificationSchedule({
date: new Date(Date.now() + 30 * 1000), // in 30 secs
Expand All @@ -133,10 +133,10 @@ export default class NotifService {
when: null, // (optionnal) Add a timestamp pertaining to the notification (usually the time the event occurred). For apps targeting Build.VERSION_CODES.N and above, this time is not shown anymore by default and must be opted into by using `showWhen`, default: null.
usesChronometer: false, // (optional) Show the `when` field as a stopwatch. Instead of presenting `when` as a timestamp, the notification will show an automatically updating display of the minutes and seconds since when. Useful when showing an elapsed time (like an ongoing phone call), default: false.
timeoutAfter: null, // (optional) Specifies a duration in milliseconds after which this notification should be canceled, if it is not already canceled, default: null

/* iOS only properties */
category: '', // (optional) default: empty string

/* iOS and Android properties */
id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
title: 'Scheduled Notification', // (optional)
Expand All @@ -145,6 +145,7 @@ export default class NotifService {
playSound: !!soundName, // (optional) default: true
soundName: soundName ? soundName : 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
number: 10, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
fullScreenIntent,
});
}

Expand Down
6 changes: 5 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />

<application
android:name=".MainApplication"
Expand All @@ -20,7 +21,10 @@
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
android:windowSoftInputMode="adjustResize"
android:showOnLockScreen="true"
android:showWhenLocked="true"
android:turnScreenOn="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
25 changes: 25 additions & 0 deletions example/android/app/src/main/java/com/example/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.example;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.PowerManager;
import android.view.WindowManager;

import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {
Expand All @@ -12,4 +17,24 @@ public class MainActivity extends ReactActivity {
protected String getMainComponentName() {
return "example";
}

@Override
protected void onStart() {
super.onStart();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
} else {
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "myapp:wakeLock");
wl.acquire();

getWindow().addFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
);
}
}
}
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"@react-native-community/push-notification-ios": "^1.7.0",
"react": "16.13.1",
"react-native": "0.63.3",
"react-native-push-notification": "zo0r/react-native-push-notification#dev"
"react-native-push-notification": "Change line below back to 'zo0r/react-native-push-notification#dev' once this brnach is merged to the dev branch.",
"react-native-push-notification": "https://github.com/mateyFromTheBlock/react-native-push-notification.git#adding-full-screen-intent-support"
},
"devDependencies": {
"@babel/core": "^7.9.0",
Expand Down